【发布时间】:2016-08-29 17:08:45
【问题描述】:
我正在使用 NSUserDefaults 来存储(字符串)数组,并且在加载它时,它似乎被解释为 AnyObject 而不是数组。我不明白这是怎么可能的,因为我使用 arrayForKey 方法作为我的默认方法,我认为它应该返回一个数组?
我得到的确切错误是:
“[AnyObject]?”类型的值没有成员“removeAtIndex”
发生在shoppingListDefaults.arrayForKey("ShoppingList").removeAtIndex(indexPath.row)
let shoppingListDefaults = NSUserDefaults.standardUserDefaults()
let deleteAction = UITableViewRowAction(style: .Normal, title: "Delete") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
shoppingListDefaults.arrayForKey("ShoppingList").removeAtIndex(indexPath.row)
self.slItems.reloadData() // `slItems` is the IBOutlet for a UITableView
}
deleteAction.backgroundColor = UIColor.redColor()
return [deleteAction]
【问题讨论】:
标签: ios arrays swift persistence nsuserdefaults