如何在swift中将数组转换为JSON字符串?基本上我有一个嵌入按钮的文本字段.按下按钮时,文本字段文本将添加到testArray.此外,我想将此数组转换为JSON字符串.
这是我尝试过的:
func addButtonPressed() {
    if goalsTextField.text == "" {
        // Do nothing
    } else {
        testArray.append(goalsTextField.text)
        goalsTableView.reloadData()
        saveDatatoDictionary()
    }
}
func saveDatatoDictionary() {
    data = NSKeyedArchiver.archivedDataWithRootObject(testArray)
    newData = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(), error: nil) as? NSData
    string = NSString(data: newData!, encoding: NSUTF8StringEncoding) 
    println(string)
}
Run Code Online (Sandbox Code Playgroud)
我还想使用我的方法返回JSON字符串savetoDictionart().
我正在尝试使用Swift为CAGradientLayer设置动画.对于我正在使用这篇文章的资源
这是我的代码
class ViewController: UIViewController {
  var gradient : CAGradientLayer?;
  override func viewDidLoad() {
    super.viewDidLoad()
  }
  override func viewDidAppear(animated: Bool) {
    self.gradient = CAGradientLayer()
    self.gradient?.frame = self.view.bounds
    self.gradient?.colors = [ UIColor.redColor().CGColor, UIColor.redColor().CGColor]
    self.view.layer.insertSublayer(self.gradient, atIndex: 0)
    animateLayer()
  }
  func animateLayer(){
    var fromColors = self.gradient?.colors
    var toColors: [AnyObject] = [ UIColor.blueColor().CGColor, UIColor.blueColor().CGColor]
    var animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")
    animation.fromValue = fromColors
    animation.toValue = toColors
    animation.duration = 3.00
    animation.removedOnCompletion = true
    animation.fillMode = kCAFillModeForwards
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    animation.delegate = …Run Code Online (Sandbox Code Playgroud) 我试图通过Button动作呈现UIImagePickerController.我的项目因错误而崩溃:
执行本机代码时获得SIGABRT.这通常表示单声道运行时或应用程序使用的本机库之一出现致命错误.
我只在Storyboard中的NavigationController中嵌入了一个ViewController.代码片段如下:
UIImagePickerController imagePicker;
public override void ViewDidLoad()
{
    base.ViewDidLoad();
    this.setupImagePicker();
    CapturePhotoButton.TouchUpInside += delegate
    {
        this.AddMedia();
    };
}
public void setupImagePicker()
{
    imagePicker = new UIImagePickerController();
    imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
    imagePicker.ModalPresentationStyle = UIModalPresentationStyle.Popover;
    imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes(
        UIImagePickerControllerSourceType.Camera);
    imagePicker.FinishedPickingMedia += HandleFinishedPickingMedia;
    imagePicker.Canceled += (sender, e) => {
        imagePicker.DismissModalViewController(true);
    };
}
public void HandleFinishedPickingMedia(object sender, 
    UIImagePickerMediaPickedEventArgs e)
{
    bool isImage = false;
    switch (e.Info[UIImagePickerController.MediaType].ToString())
    {
        case "public.image":
            isImage = true;
            break;
        case "public.video":
            break;
    }
    if (isImage)
    {
        UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] …Run Code Online (Sandbox Code Playgroud) 我有一个带有 UICollectionView 的 UIViewController,它不是我的应用程序的根,而是通过 segue 到达的。这个 UICollectionView 有 UICollectionViewCells 有一个带有 UITapGestureRecognizer 的 imageView,当触发时它有助于呈现一个 UIAlertViewController。当我收到此警告时,我在测试我的 show alert 方法时遇到了麻烦:
“尝试在视图不在窗口层次结构中的 * 上呈现!”
我的代码片段写在下面
class ViewControllerTests : XCTestCase {
var vc : ViewController!
override func setUp() {
    super.setUp()
    let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    vc = storyboard.instantiateViewControllerWithIdentifier("LocationsVC") as! ViewController
    vc.loadView()
}
override func tearDown() {
    super.tearDown()
}
func testshowLocationActionSheet(){
    vc.viewDidLoad()
    vc.viewDidAppear(true)
    vc.collectionView.reloadData()
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    var cell = vc.collectionView(vc.collectionView, cellForItemAtIndexPath: indexPath) as! FranchiseLocatorViewCell
    XCTAssertNotNil(cell)
    cell = vc.collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: …Run Code Online (Sandbox Code Playgroud) 当我通过模拟器上的调试运行我的应用程序时,我一直在处理这个问题,只要我升级到最新的Xcode 8.0和OS版本10.11.6.错误如下:
应用程序设置例外,无法存储密钥CognitoIdentity:IdentityId:eu-west-1:KEY,得到错误:-34018.
似乎它与钥匙串有关,但还没有得到Xamarin的解决方案.