苹果网站的食物跟踪器应用程序

时间:2015-08-13 16:52:57

标签: ios swift

  

错误:无法下标'[String:AnyOject]类型的值?'与   'String'类型的索引

我一直在尝试遵循this developers site中的教程:但我似乎无法使此代码正常工作。

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    let selectedImage = editingInfo[SUIImagePickerControllerOriginalImage] as! UIImage
    // Set photoImageView to selected image
    photoImageView.image = selectedImage

3 个答案:

答案 0 :(得分:1)

swift 1.2中的方法改变Apple没有修改他们的文档

您将功能更改为以下功能:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

}

它应该可行

答案 1 :(得分:0)

editingInfo是一个Optional,由尾部'?'表示。你应该通过执行if let:

来绑定它
if let dict = editingInfo {
    let selectedImage = dict[SUIImagePickerControllerOriginalImage] as! UIImage
    photoImageView.image = selectedImage
}
else {
     // Handle the case where editingInfo is nil
}

答案 2 :(得分:0)

对于延迟响应感到抱歉,代码似乎有效。我使用了两个答案,似乎教程的格式化已经发生了变化。

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, info: [String : AnyObject]!) {

    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

     //Set photoImageView to selected image
    photoImageView.image = selectedImage

    // Dismiss the picker
    dismissViewControllerAnimated(true, completion: nil)