我是iOS开发的新手。我想知道一件事我将一个字符串附加到NSmenged对象中它给我错误请解决它
这是我的代码 -
let json1 = try NSJSONSerialization.JSONObjectWithData(response.data!, options:.AllowFragments)
let json2 = json1["interests"] as! NSArray
// print(json2)
for var i=0; i<json2.count; i++
{
let object = json2[i] as! NSDictionary
//print(object)
let name = object["name"] as! NSString
print(name)
self.names.append(name as! NSManagedObject)
}
在最后一行我收到错误。
答案 0 :(得分:0)
试试这个:
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
let newContact = NSEntityDescription.insertNewObjectForEntityForName("Contact", inManagedObjectContext: context)
newContact.setValue(txtName.text, forKey: "name")
do {
try context.save()
} catch {
print("Save error!")
}