質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1450閲覧

swift3 GestureRecognizerとAlertの組み合わせによるエラーについて

asuuuuuuuuu7

総合スコア16

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/06/02 10:27

下記のようにコードを書いていますが、buildingすると libc++abi.dylib: terminating with uncaught exception of type NSException というエラー(?)が出てしまいます。

何か原因かわかる方助けていただければ幸いです。

swift3

1func changeColor(_ sender: UITapGestureRecognizer) { selectImage()} 2 3 4 5 private func selectImage() { 6 let alertController = UIAlertController(title: "画像を選択", message: nil, preferredStyle: .actionSheet) 7 let cameraAction = UIAlertAction(title: "写真を撮る", style: .default) { (UIAlertAction) -> Void in 8 self.selectFromCamera() 9 } 10 let libraryAction = UIAlertAction(title: "カメラロールから選択", style: .default) { (UIAlertAction) -> Void in 11 self.selectFromLibrary() 12 } 13 let cancelAction = UIAlertAction(title: "キャンセル", style: .cancel) { (UIAlertAction) -> Void in 14 self.dismiss(animated: true, completion: nil) 15 } 16 alertController.addAction(cameraAction) 17 alertController.addAction(libraryAction) 18 alertController.addAction(cancelAction) 19 present(alertController, animated: true, completion: nil) 20 } 21 22 private func selectFromCamera() { 23 if UIImagePickerController.isSourceTypeAvailable(.camera) { 24 let imagePickerController = UIImagePickerController() 25 imagePickerController.delegate = self 26 imagePickerController.sourceType = UIImagePickerControllerSourceType.camera 27 imagePickerController.allowsEditing = true 28 self.present(imagePickerController, animated: true, completion: nil) 29 } else { 30 print("カメラ許可をしていない時の処理") 31 } 32 } 33 34 private func selectFromLibrary() { 35 if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { 36 let imagePickerController = UIImagePickerController() 37 imagePickerController.delegate = self 38 imagePickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary 39 imagePickerController.allowsEditing = true 40 self.present(imagePickerController, animated: true, completion: nil) 41 } else { 42 print("カメラロール許可をしていない時の処理") 43 } 44 } 45 46 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 47 48 if let image = info[UIImagePickerControllerEditedImage] { 49 self.showAlert.image = image as? UIImage 50 } 51 52 picker.dismiss(animated: true, completion: nil) 53 } 54

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Bongo

2017/06/02 22:37 編集

デバッグエリアの「libc++abi.dylib: terminating with uncaught exception of type NSException」と表示されるより前の行に、もし「Terminating app due to uncaught exception ...」などと書かれた部分があれば、そちらもご提示いただけるでしょうか。
asuuuuuuuuu7

2017/06/02 23:47

erminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setImage:]: unrecognized selector sent to instance 0x135e5a400' *** First throw call stack: (0x1820a9900 0x181717f80 0x1820b061c 0x1820ad5b8 0x181fb168c 0x10003574c 0x100035b6c 0x18724b3d4 0x18c7ecfa8 0x18c830cb8 0x18a7131a4 0x1015a1bb0 0x1015b21b4 0x1015a1bb0 0x1015a7658 0x182060bb0 0x18205ea18 0x181f8d680 0x18349c088 0x186e04d90 0x100044a8c 0x181b2e8b8) このように出ております。
guest

回答1

0

ベストアンサー

Objective-Cに基づくメッセージのため分かりづらかったかもしれませんが、「UIViewオブジェクトのsetImage:という名前のメソッドを探したが見つからなかった」というような意味合いです。
怪しいのはself.showAlert.image = image as? UIImageの部分ですが、変数showAlertの宣言部分の型がUIImageViewになっているか(現状UIViewになっていませんか?)ということと、そこに代入されるオブジェクトがUIImageViewであるか(あるいは、接続したストーリーボード上のビューがUIImageViewになっているか)をご確認ください。

[追記]
宣言時の型がUIViewならSwiftがエラーを出すはずですから、確認するべきは後者の可能性でしょうか。特にストーリーボードのアウトレットの接続状態が気になります。

投稿2017/06/03 00:12

編集2017/06/03 00:24
Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

asuuuuuuuuu7

2017/06/03 00:31

@IBOutlet weak var showAlert: UIImageView! とimageViewをアウトレット接続しております、これだとダメなんでしょうか…?
Bongo

2017/06/03 00:45

問題はストーリーボード側かと思います。現在の@IBOutlet weak var showAlert: UIImageView!の行は削除してしまい、またストーリーボード上のshowAlertも削除して、あらためてストーリーボードにUIImageViewを配置し、Swiftコード側に再接続してみてください。 これで自動的にSwift側の変数の型がUIImageViewになれば、おそらくうまくいっています。
asuuuuuuuuu7

2017/06/03 01:45

ありがとうございます、接続し直したらつながりました…!!! 本当にありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問