我正在使用Apple's Swift iOS Tutorial。哪个抛出错误,
不能使用索引类型为“ UIImagePickerController.InfoKey”的索引对类型为[[String:Any]”的值进行下标
他们定义的功能如下。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
我正在使用Xcode版本10.0 beta 3,其中包括Swift 4.2。
我想了解如何遍历文档以了解可能已更改或损坏的内容。
答案 0 :(得分:78)
该方法的签名在Swift 4.2中已更改
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
你必须写
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
您可以通过阅读documentation或注释掉整个方法,重新键入前几个字符并使用代码完成来找出自己的术语更改。
答案 1 :(得分:17)
我也遵循同一教程,更新后的代码如下:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
答案 2 :(得分:5)
像这样使用
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage.rawValue] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
答案 3 :(得分:2)
在Swift 4和5中,如下所示:
const perform = parentId => groupClassName => prototype => callback => {
const groups = Array.from(document.getElementById(parentId).getElementsByClassName(groupClassName));
return prototype.call(groups, callback);
};
答案 4 :(得分:2)
我也在关注本教程。如下更改后,它可以工作。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as? UIImage
else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
我正在使用XCode 11和Swift 5。
答案 5 :(得分:1)
在Swift 4.2中,方法进行了一些更改。
首先初始化这两个变量:
var imagePicker = UIImagePickerController()
var pickedImageProduct = UIImage()
extension yourViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate{
//create an IBAction to access Camera
@IBAction func accessCameraBtn(_ sender: UIButton) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.camera
self.present(imagePicker, animated: true, completion: nil)
}
//create an IBAction to access Gallery
@IBAction func accessGalleryBtn(_ sender: UIButton) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
self.present(imagePicker, animated: true, completion: nil)
}
//Final step put this Delegate
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[.originalImage] as? UIImage else {
Print("Error: \(info)")
}
pickedImageProduct = selectedImage
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
答案 6 :(得分:1)
在Swift 4.2中,您可以将函数编写为:
func photoLib()
{
//opens Photo Library, call the function in a @IBAction func
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType =
UIImagePickerController.SourceType.photoLibrary
myPickerController.allowsEditing = true
present(myPickerController, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true)
guard let image = info[.editedImage] as? UIImage else {
print("No image found")
photoLabel.text = "Photo Not Found"
return
}
}
答案 7 :(得分:0)
在最新版本的swift 4或5中,下面给出了委托方法,它应该可以工作
open()
并使用
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// Code here
}