我想将Int保存到我的核心数据中,现在我这样做但它在Int上崩溃并且只保存了1个项目。
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext!
let ent = NSEntityDescription.entityForName("Groups", inManagedObjectContext: context)
var newItem = Groups(entity: ent!, insertIntoManagedObjectContext: context)
我的代码
if let groups : AnyObject! = json {
//println(groups["groups"])
var group = groups["groups"] as NSArray
for g in group{
newItem.id = g["id"] as Int
newItem.name = g["name"] as NSString
context.save(nil)
println(newItem)
println(g["id"] as Int)
//println(g["name"] as NSString)
}
}
型号:
@objc(Groups)
class Groups: NSManagedObject {
@NSManaged var id:Int
@NSManaged var name:String
}
Consolelog:
nil
<NSManagedObject: 0x7fe0c9f2f520> (entity: Groups; id: 0xd000000000140000 <x-coredata://1405B706-2AC6-4CED-BED5-FE0232723976/Groups/p5> ; data: {
"group_id" = nil;
id = 0;
name = Press;
})
1
<NSManagedObject: 0x7fe0c9f2f520> (entity: Groups; id: 0xd000000000140000 <x-coredata://1405B706-2AC6-4CED-BED5-FE0232723976/Groups/p5> ; data: {
"group_id" = nil;
id = 0;
name = Sales;
})
2
<NSManagedObject: 0x7fe0c9f2f520> (entity: Groups; id: 0xd000000000140000 <x-coredata://1405B706-2AC6-4CED-BED5-FE0232723976/Groups/p5> ; data: {
"group_id" = nil;
id = 0;
name = "Other information";
})
3