2017-12-09 15 views
-1

私はTo-do-listアプリケーションを構築していました。そして私は、私のアプリがクラッシュを「タスクを追加」ボタンを押して、私に次のエラー与えたとき:[__NSCFArray insertObject:atIndex:]:変更メソッドが不変オブジェクトに送信されました '、スレッド:SIGABRT

[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'

それはまた、私はSIGABRTエラーをスレッドに返します。どんな助けもありがとう。

は、ここに私のSecondViewController

class SecondViewController: UIViewController, UITextFieldDelegate { 
    @IBOutlet weak var taskValue: UITextField! 
    @IBAction func addTask(_ sender: Any) { 
     let itemsObject = UserDefaults.standard.object(forKey: "items") 

     var items:NSMutableArray! 

     if let tempItems = itemsObject as? NSMutableArray{ 
      items = tempItems 
      items.addObjects(from: [taskValue.text!]) 
     } else{ 
      items = [taskValue.text!] 
     } 

     UserDefaults.standard.set(items, forKey: "items") 

     taskValue.text = "" 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     self.view.endEditing(true) 
    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 
     return true 
    } 
} 

私の最初のビューコントローラがここにあります:あなたはNSMutableArrayにスウィフトの配列をキャストすることはできませんので

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{ 
    var items: NSMutableArray = [] 

    //table view 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return items.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cellContent = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell") 

     var cellLabel = "" 

     if let tempLabel = items[indexPath.row] as? String{ 
      cellLabel = tempLabel 
     } 
     cellContent.textLabel?.text = cellLabel 
     return cellContent 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let itemsObject = UserDefaults.standard.object(forKey: "items") 

     if let tempItems = itemsObject as? NSMutableArray{ 
      items = tempItems 
     } 
    } 
} 
+1

Swift配列ではなくNSMutableArrayを使用している理由は何ですか? – Grimxn

+0

@Grimxnあなたは何をお勧めしますか?私はSwift/iOS開発の初心者です –

+3

このドキュメントはhttps://developer.apple.com/documentation/swift/arrayにあります。Swiftには通常NSArray/NSMutableArrayを使用する必要はありません。 – Grimxn

答えて

1

エラーが発生しました。 SwiftにFoundationタイプのNSMutable...を使用しないでください。

変更可能なオブジェクトを取得するのは非常に簡単です。varキーワードを使用してください。

UserDefaultsには、文字列配列を取得する専用のメソッドがあります。

@IBAction func addTask(_ sender: Any) { 

    var items : [String] 
    if let itemsObject = UserDefaults.standard.stringArray(forKey: "items") { 
     items = itemsObject 
     items.append(taskValue.text!) // it's pointless to use the API to append an array. 
    } else{ 
     items = [taskValue.text!] 

    } 
    UserDefaults.standard.set(items, forKey: "items") 
    taskValue.text = "" 

} 
+0

これは、次のようにより簡潔に書くことができます: 'var items = UserDefaults.standard.stringArray(forKey:" items ")?? []に続いて 'items.append(taskValue.text!)'が続きます。 2行対7行。 – rmaddy

関連する問題