持久性存储错误 - 格式列表无效:200

时间:2015-01-20 15:11:17

标签: swift

该应用程序是一个类似于待办事项列表应用程序的列表。它工作正常但是当我添加关于持久数据(NSUser默认值)的代码时,我遇到了错误。我有错误和我的代码如下。 (我评论过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] ***终止app由于   未捕获的异常'NSInvalidArgumentException',原因:'尝试   为键插入非属性列表对象(“”,“”)   shoppingList'

代码:     导入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()
}
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下模式简单地保存/恢复设置:

let kString = "string"
let kInt = "int"

我在全球范围内声明这些常量。 Swift没有#define。另外,我用相关类的驼峰案例字符来扩充这些常量。例如。对于class MyFancyClass,我会将第一个命名为kMFCstring

NSUserDefaults.standardUserDefaults().setObject("string data", forKey: kString)
NSUserDefaults.standardUserDefaults().setInteger(4711, forKey: kInt)

这会将“字符串数据”和4711保存为默认值。请注意,int,float,object等有不同的方法。请参阅帮助或自动完成。还有一种同步方法可以强制将设置保存到文件中,但操作系统会在有用时自动执行此操作(例如,当程序终止时)。

let stringVal = NSUserDefaults.standardUserDefaults().objectForKey(kString)! as String
let intVal = NSUserDefaults.standardUserDefaults().integerForKey(kInt)! as String

这将恢复值。这与以前的设置方法完全相反。

对于非对象的类型,您可以创建容器。一种可能性是使用NSValue

NSUserDefaults.standardUserDefaults().setObject(NSValue(NSMakeRect(1,2,3,4), forKey: "myRect")

查看NSValue内部可以打包的其他内容的帮助。