【发布时间】:2014-10-14 07:58:33
【问题描述】:
我正在快速尝试基于核心数据的应用程序,其中我正在执行以下步骤:
- 从 plist 中检索数据
- 遍历检索到的数据
- 在每次迭代期间,在 managedObjectContext 中插入一个 managedObject
- 在字典中插入的 managedObject 中设置值
下面是我正在使用的代码:
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedObjectContext = appDelegate.managedObjectContext!
// 1. Retrieve data from plist
var defaultDataPlistPath = NSBundle.mainBundle().pathForResource("DefaultData", ofType: "plist")
var defaultDataArray = NSArray(contentsOfFile: defaultDataPlistPath!)
// 2. Store retrieved data in local db using core data apis
for menuCategoryDict in defaultDataArray {
// storing menu categories
var attributes = menuCategoryDict["attributes"]
var menuCategory : MenuCategories = NSEntityDescription.insertNewObjectForEntityForName("MenuCategories", inManagedObjectContext: managedObjectContext) as MenuCategories
menuCategory.setValuesForKeysWithDictionary(attributes) // this line is giving compilation error :(
}
appDelegate.saveContext()
问题是 - 由于某些原因,我收到编译错误:'AnyObject?'在这一行与 '[NSObject : AnyObject]' 不同:
menuCategory.setValuesForKeysWithDictionary(attributes)
我一无所知,请建议。
【问题讨论】:
标签: ios swift nsmanagedobject kvc