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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

8019閲覧

【エラーの対処法がわかりません】exc_bad_instruction (code=exc_i386_invop subcode=0x0)

YamasakiRintaro

総合スコア13

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2016/08/01 14:22

初心者です。
todoアプリを以下のサイトをもとに作成しています。
todoアプリチュートリアル

シュミレーター実行時にOKを押下すると、ランタイムエラーで落ちます。

調べてもよくわかりませんでした。
よろしくお願いします。

@IBAction func tapAddBtn(sender: AnyObject) { let alertController = UIAlertController(title: "ラベル追加", message: "ラベルを入力してください", preferredStyle: UIAlertControllerStyle.Alert) alertController.addTextFieldWithConfigurationHandler(nil) let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action: UIAlertAction) -> Void in if let textField = alertController.textFields?.first { let data = Label() data.title = textField.text! self.labelList.insert(data, atIndex: 0) //以下にエラーが出る //exc_bad_instruction (code=exc_i386_invop subcode=0x0) self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Right) self.setLabelList() } } alertController.addAction(okAction) let cancelAction = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.Cancel, handler: nil) alertController.addAction(cancelAction) presentViewController(alertController, animated: true, completion: nil) } ```![イメージ説明](b282a6628514342528cec93e55817fda.png)

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

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

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

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

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

guest

回答1

0

ベストアンサー

おそらくですが、参考にしているサイトの一つ前の段階での工程でUITableViewCellに対してIdentifierのところにlabelCellと入力する。というところの作業が抜けているでは無いでしょうか?
確認してください。

0から始めるiOS開発③: TODOアプリ開発(前編)

エラー時のコンソールログに以下の様なものがあるのではないでしょうか。違う場合はエラーメッセージも載せて頂くと適切なアドバイスができると思います。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier labelCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' *** First throw call stack:

こちらで確認したコード

swift

1import UIKit 2 3class ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate { 4 5 var labelList = [Label]() 6 @IBOutlet weak var tableView: UITableView! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 tableView.delegate = self 12 tableView.dataSource = self 13 14 let userDefaults = NSUserDefaults.standardUserDefaults() 15 if let labelListData = userDefaults.objectForKey(Constant.Label.LIST) as? NSData { 16 if let storedTodoList = NSKeyedUnarchiver.unarchiveObjectWithData(labelListData) as? [Label] { 17 labelList.appendContentsOf(storedTodoList) 18 } 19 } 20 } 21 22 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 23 return labelList.count; 24 } 25 26 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 27 let cell = tableView.dequeueReusableCellWithIdentifier("labelCell", forIndexPath: indexPath) 28 let label = labelList[indexPath.row] 29 cell.textLabel!.text = label.title 30 if label.done { 31 cell.accessoryType = UITableViewCellAccessoryType.Checkmark 32 } else { 33 cell.accessoryType = UITableViewCellAccessoryType.None 34 } 35 return cell 36 } 37 38 39 @IBAction func tapAddBtn(sender: UIButton) { 40 let alertController = UIAlertController(title: "ラベル追加", message: "ラベルを入力してください", preferredStyle: UIAlertControllerStyle.Alert) 41 alertController.addTextFieldWithConfigurationHandler(nil) 42 let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { 43 (action: UIAlertAction) -> Void in 44 if let textField = alertController.textFields?.first { 45 46 let data = Label() 47 data.title = textField.text 48 self.labelList.insert(data, atIndex: 0) 49 self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Right) 50 self.setLabelList() 51 } 52 } 53 alertController.addAction(okAction) 54 let cancelAction = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.Cancel, handler: nil) 55 alertController.addAction(cancelAction) 56 presentViewController(alertController, animated: true, completion: nil) 57 } 58 59 private func setLabelList() { 60 let data :NSData = NSKeyedArchiver.archivedDataWithRootObject(self.labelList) 61 let userDefaults = NSUserDefaults.standardUserDefaults() 62 userDefaults.setObject(data, forKey: Constant.Label.LIST) 63 userDefaults.synchronize() 64 } 65} 66 67class Constant { 68 class Label { 69 static let LIST = "labelList" 70 static let TITLE_KEY = "labelTitle" 71 static let DONE_KEY = "labelDone" 72 } 73} 74 75class Label: NSObject, NSCoding { 76 var title :String? 77 var done :Bool = false 78 79 override init() { 80 81 } 82 83 required init?(coder aDecoder: NSCoder) { 84 title = aDecoder.decodeObjectForKey(Constant.Label.TITLE_KEY) as? String 85 done = aDecoder.decodeBoolForKey(Constant.Label.DONE_KEY) 86 } 87 88 func encodeWithCoder(aCoder: NSCoder) { 89 aCoder.encodeObject(title, forKey: Constant.Label.TITLE_KEY) 90 aCoder.encodeBool(done, forKey: Constant.Label.DONE_KEY) 91 } 92}

投稿2016/08/01 15:29

編集2016/08/01 16:37
_Kentarou

総合スコア8490

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

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

YamasakiRintaro

2016/08/01 15:40

ご回答ありがとうございます。 Identifierを修正しても同様のエラーが出ました。 コンソールは以下の通りです。 *** 2016-08-02 00:37:21.400 0730_todoapp[25584:4058233] The relevant UICollectionViewFlowLayout instance is <_UIAlertControllerCollectionViewFlowLayout: 0x7f863bd7f320>, and it is attached to <UICollectionView: 0x7f863d04c800; frame = (0 120.667; 270 44); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f863bd800d0>; layer = <CALayer: 0x7f863bd08b40>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <_UIAlertControllerCollectionViewFlowLayout: 0x7f863bd7f320>. 2016-08-02 00:37:21.401 0730_todoapp[25584:4058233] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger. fatal error: unexpectedly found nil while unwrapping an Optional value ***
_Kentarou

2016/08/01 15:56

落ちている箇所は前と同じself.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Right) の場所ですか?
_Kentarou

2016/08/01 16:04 編集

シュミレーターのiPhone6で実行しても同じ結果がでるか確認してもらえますか?
_Kentarou

2016/08/01 16:06

おそらくUITableViewのIBOutletが正常に結ばれていないと思います、結び直してみてください。
YamasakiRintaro

2016/08/01 16:15

結び直すと、正常に動作しました!! _Kentarouさん、ありがとうございます!! 以下の通りにしました。 同じ記述であるのに、エラーが出ていたのがわかりません。 正常に結ばれていないってどういった事を指しているのでしょうか?? *** //@IBOutlet weak var tableView: UITableView! @IBOutlet weak var tableView: UITableView! ***
_Kentarou

2016/08/01 16:19 編集

一度結んでから自分で変数名の綴を変更したりすると結び付きが切れてしまいます。 var tableview → var tableView
YamasakiRintaro

2016/08/01 16:27

そうなんですね。 一度、シュミレーターで実行した後 再度シュミレータを立ち上げ実行してみると、 起動時に落ちてしまい、 AppDelegate.swiftのクラス名定義の箇所で、 Thread 1:signal SIGABRT が出ていました。 調べると、このエラーからこれといった解決方法は導けないそうなので、 一度最初からやり直してみようと思います。 ご丁寧な回答、本当にありがとうございました。
_Kentarou

2016/08/01 16:36

エラーメッセージが出力されているのであれば原因はわかると思いますよ。 UITableViewのdelegate、dataSourceはselfに設定されているか確認してみてください。 こちらで確認したコードも回答に追記しておきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問