내가 할 일 목록 응용 프로그램을 작성 중입니다. 그리고 나는 내 응용 프로그램 충돌을 "작업 추가"버튼을 눌러 나에게 다음과 같은 오류 줄 때 :[__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
}
}
}
스위프트 배열이 아닌 NSMutableArray를 사용하는 이유는 무엇입니까? – Grimxn
@Grimxn 무엇을 권하고 싶습니까? Swift/iOS 개발에 익숙하지 않습니다. –
문서는 https://developer.apple.com/documentation/swift/array에 있습니다. Swift에서 NSArray/NSMutableArray를 사용할 이유가 없습니다. – Grimxn