【发布时间】:2015-01-21 18:43:06
【问题描述】:
该应用是一个列表,非常类似于“待办事项列表”应用。它工作正常,但是当我添加关于持久数据(NSUser Defaults)的代码时,我遇到了一个错误。我有错误和下面的代码。 (我已经评论了 NSUserdefaults 部分)。我希望你能指导我并提供一些答案。
错误:2015-01-19 22:26:40.783 Shoplisters[62316:22301266] 属性 列表格式无效:200(属性列表不能包含 类型 'CFType') 2015-01-19 22:26:40.783 Shoplisters[62316:22301266] 尝试将非属性列表对象 ( "", "" ) 设置为 关键 shoppingList 的 NSUserDefaults/CFPreferences 值 2015-01-19 22:26:40.785 Shoplisters[62316:22301266] *** 由于以下原因而终止应用程序 未捕获的异常“NSInvalidArgumentException”,原因:“尝试 为键插入非属性列表对象(“”,“”) 购物清单'
代码: 导入 UIKit
var shoppingList:[ShopList] = listData
class ShopListTableViewController: UITableViewController {
@IBAction func cancelToListView(segue:UIStoryboardSegue) {
dismissViewControllerAnimated(true, completion: nil)
//actions for cancel button
}
@IBAction func saveListDetail(segue:UIStoryboardSegue) {
let listDetailsViewController = segue.sourceViewController as AddNewListTableViewController
//add the new list to the shopping list array
shoppingList.append(listDetailsViewController.listDetails)
//NSUserdefaults
//let fixedShoppingList = shoppingList
//NSUserDefaults.standardUserDefaults().setObject(fixedShoppingList, forKey: "shoppingList")
//NSUserDefaults.standardUserDefaults().synchronize()
//update the table view
let indexPath = NSIndexPath(forRow: shoppingList.count-1, inSection: 0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
//hide the detail view controller
dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
//if NSUserDefaults.standardUserDefaults().objectForKey("shoppingList") != nil {
//shoppingList = NSUserDefaults.standardUserDefaults().objectForKey("shoppingList") as [ShopList]
//}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return shoppingList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("listCell", forIndexPath: indexPath) as ShoppingListTableViewCell
//fill the prototype cell with the ff values:
var list = shoppingList[indexPath.row] as ShopList
cell.listNameLabel.text = list.name
cell.totalBudgetLabel.text = list.budget
cell.barColor.image = imageForColors(list.barColors)
return cell
}
数据:
import Foundation
import UIKit
class ShopList: NSObject {
var name:String
var budget:String
var barColors:String
init(name:String, budget:String, barcolor:String) {
self.name = name
self.budget = budget
self.barColors = barcolor
super.init()
}
}
【问题讨论】:
-
您只能将元素放入符合 NSCoding 的 NSUserDefaults 中。 CFType 没有。你需要把它包装成例如NSData 之类的。
-
感谢您的回复。我实际上是 iOS 新手,而且很快。您可以为我现有的代码提供一个示例吗?不知道该怎么做。
标签: swift