使用 tableView 滑动功能从 NSUserDefaults 中删除项目时出错
Error while removing items from NSUserDefaults with tableView swipe function
我尝试使用 tableView 滑动功能从 NSUserDefaults 中删除项目。但是当我测试我的应用程序时,它总是在单击 "delete".
后崩溃
定义变量并从 NSUserDefaults 加载数据:
var exercises:NSMutableArray = NSMutableArray();
...
override func viewDidLoad() {
super.viewDidLoad()
var userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
var exercisesListFromUserDefaults:NSMutableArray? = userDefaults.objectForKey("exercisesList") as? NSMutableArray
if ((exercisesListFromUserDefaults) != nil){
exercises = exercisesListFromUserDefaults!
}
self.tableView.reloadData()
}
删除函数:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
self.deleteItem(exercises[indexPath.row])
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
func deleteItem(sender: AnyObject) {
exercises.removeObject(sender)
var userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(exercises, forKey: "exercisesList")
userDefaults.synchronize()
}
编辑: 错误:
(lldb) BT
* 线程 #1:tid = 0x190c34,0x0000000108715282 libsystem_kernel.dylib__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x0000000108715282 libsystem_kernel.dylib
__pthread_kill + 10
第 1 帧:0x00000001087484c3 libsystem_pthread.dylibpthread_kill + 90
frame #2: 0x00000001084a519a libsystem_sim_c.dylib
中止 + 129
第 3 帧:0x0000000108759481 libc++abi.dylibabort_message + 257
frame #4: 0x00000001087813d5 libc++abi.dylib
default_terminate_handler() + 267
第 5 帧:0x0000000107c1de19 libobjc.A.dylib_objc_terminate() + 103
frame #6: 0x000000010877eb01 libc++abi.dylib
std::__terminate(void (*)()) + 8
第 7 帧:0x000000010877e7aa libc++abi.dylib__cxa_rethrow + 99
frame #8: 0x0000000107c1dd2c libobjc.A.dylib
objc_exception_rethrow + 40
第 9 帧:0x000000010600453e CoreFoundationCFRunLoopRunSpecific + 654
frame #10: 0x000000010a1d09f0 GraphicsServices
GSEventRunModal + 161
第 11 帧:0x0000000106965420 UIKitUIApplicationMain + 1282
* frame #12: 0x0000000105ee2c5e Progress
top_level_code + 78 在 AppDelegate.swift:12
第 13 帧:0x0000000105ee2c9a 进度main + 42 at AppDelegate.swift:0
frame #14: 0x00000001083f7145 libdyld.dylib
开始 + 1
这一行
var exercisesListFromUserDefaults:NSMutableArray? = userDefaults.objectForKey("exercisesList") as? NSMutableArray
是个问题,因为数组不会可变。用户默认始终 returns 不可变对象。您需要先创建此数组的可变数组(副本),然后才能更改它并将其存储回用户默认值。
var myMutableArray = NSMutableArray(array: myImmutableArray)
我尝试使用 tableView 滑动功能从 NSUserDefaults 中删除项目。但是当我测试我的应用程序时,它总是在单击 "delete".
后崩溃定义变量并从 NSUserDefaults 加载数据:
var exercises:NSMutableArray = NSMutableArray();
...
override func viewDidLoad() {
super.viewDidLoad()
var userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
var exercisesListFromUserDefaults:NSMutableArray? = userDefaults.objectForKey("exercisesList") as? NSMutableArray
if ((exercisesListFromUserDefaults) != nil){
exercises = exercisesListFromUserDefaults!
}
self.tableView.reloadData()
}
删除函数:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
self.deleteItem(exercises[indexPath.row])
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
func deleteItem(sender: AnyObject) {
exercises.removeObject(sender)
var userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(exercises, forKey: "exercisesList")
userDefaults.synchronize()
}
编辑: 错误:
(lldb) BT
* 线程 #1:tid = 0x190c34,0x0000000108715282 libsystem_kernel.dylib__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x0000000108715282 libsystem_kernel.dylib
__pthread_kill + 10
第 1 帧:0x00000001087484c3 libsystem_pthread.dylibpthread_kill + 90
frame #2: 0x00000001084a519a libsystem_sim_c.dylib
中止 + 129
第 3 帧:0x0000000108759481 libc++abi.dylibabort_message + 257
frame #4: 0x00000001087813d5 libc++abi.dylib
default_terminate_handler() + 267
第 5 帧:0x0000000107c1de19 libobjc.A.dylib_objc_terminate() + 103
frame #6: 0x000000010877eb01 libc++abi.dylib
std::__terminate(void (*)()) + 8
第 7 帧:0x000000010877e7aa libc++abi.dylib__cxa_rethrow + 99
frame #8: 0x0000000107c1dd2c libobjc.A.dylib
objc_exception_rethrow + 40
第 9 帧:0x000000010600453e CoreFoundationCFRunLoopRunSpecific + 654
frame #10: 0x000000010a1d09f0 GraphicsServices
GSEventRunModal + 161
第 11 帧:0x0000000106965420 UIKitUIApplicationMain + 1282
* frame #12: 0x0000000105ee2c5e Progress
top_level_code + 78 在 AppDelegate.swift:12
第 13 帧:0x0000000105ee2c9a 进度main + 42 at AppDelegate.swift:0
frame #14: 0x00000001083f7145 libdyld.dylib
开始 + 1
这一行
var exercisesListFromUserDefaults:NSMutableArray? = userDefaults.objectForKey("exercisesList") as? NSMutableArray
是个问题,因为数组不会可变。用户默认始终 returns 不可变对象。您需要先创建此数组的可变数组(副本),然后才能更改它并将其存储回用户默认值。
var myMutableArray = NSMutableArray(array: myImmutableArray)