2017-12-09 38 views
-1

내가 할 일 목록 응용 프로그램을 작성 중입니다. 그리고 나는 내 응용 프로그램 충돌을 "작업 추가"버튼을 눌러 나에게 다음과 같은 오류 줄 때 :[__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

스위프트 배열이 아닌 NSMutableArray를 사용하는 이유는 무엇입니까? – Grimxn

+0

@Grimxn 무엇을 권하고 싶습니까? Swift/iOS 개발에 익숙하지 않습니다. –

+3

문서는 https://developer.apple.com/documentation/swift/array에 있습니다. Swift에서 NSArray/NSMutableArray를 사용할 이유가 없습니다. – Grimxn

답변

1

오류가 발생합니다. 스위프트에 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