【问题标题】:Persistent Storage Error - Property list invalid for format: 200持久性存储错误 - 属性列表对格式无效:200
【发布时间】: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


【解决方案1】:

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

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

这将恢复值。这与之前的 set-method 正好相反。

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

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

查看NSValue 的帮助,里面还能装什么。

【讨论】:

  • 所以基本上你是说我不能使用上面的 shoppingList 变量? var shoppingList = [ShopList(name: "添加新列表", budget: "$0.00", barcolor: "")]
  • 是的。您需要转换数组中的元素,例如将它们打包成一个 NSValue 数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-21
  • 2016-06-11
相关资源
最近更新 更多