私は使用していますApple の Swift iOS チュートリアル。エラーが発生しています。
'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)
}
私は Swift 4.2 を含む Xcode バージョン 10.0 ベータ 3 を使用しています。
何が変更されたか、何が壊れているかを理解するために、ドキュメントをどのように参照すればよいか理解したいと思います。
ベストアンサー1
メソッドのシグネチャは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)")
}
このような用語の変更については、ドキュメンテーションまたは、メソッド全体をコメントアウトして、最初の数文字を再入力し、コード補完を使用します。