【发布时间】:2015-09-14 19:06:16
【问题描述】:
我开始使用 Swift 进行编程,我的项目需要 DB。我使用 Core Data 并且对此有很多问题。
最重要的问题之一是“插入数据库”,插入后我获取数据并返回一个数据列表,只有最后一个对象是好的,所有最后一个对象都是 这是我的插入、获取和获取数据的日志:
插入
let newItem = NSEntityDescription.insertNewObjectForEntityForName("DeviceList", inManagedObjectContext:managedContext) as! DeviceList
newItem.mac = deviceObject.mac
newItem.name = deviceObject.name
//4
var error: NSError?
if !managedContext.save(&error) {
println("Could not save \(error), \(error?.userInfo)")
return false
}
获取
let fetchRequest = NSFetchRequest(entityName: "DeviceList")
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [String] {
result = fetchResults
}
print("result:\(result)")
日志
"<MyAPP.DeviceList: 0x7be58fe0> (entity: DeviceList; id: 0x7be6f480 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p1> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7be600f0> (entity: DeviceList; id: 0x7be59010 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p2> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7bea5e30> (entity: DeviceList; id: 0x7be60120 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p3> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7be6e0e0> (entity: DeviceList; id: 0x7bea5e60 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p4> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7beaaa00> (entity: DeviceList; id: 0x7beac3c0 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p5> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7beaaae0> (entity: DeviceList; id: 0x7be6e150 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p6> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7beaac00> (entity: DeviceList; id: 0x7beaab50 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p7> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7bea3600> (entity: DeviceList; id: 0x7beaac30 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p8> ; data: <fault>)",
"<MyAPP.DeviceList: 0x7c03f5f0> (entity: DeviceList; id: 0x7c03c2a0 <x-coredata://72931FFE-1CAC-4A5C-BD32-817681C2439E/DeviceList/p9> ; data: {\n mac = \"00:00:00:13:43:59\";\n name = Atim;\n})"
我的插入功能有问题吗?
我还打开了 SQLiteManager (FireFox ad-one) 存储在模拟器中的 sqlite 数据库,它没有在数据库中显示任何记录,为什么?当我使用Objective-C编程时,sqlite文件显示所有数据没有任何问题!
【问题讨论】:
-
有些让我吃惊!当我通过“名称”获取“mac 设备”时,数据按预期返回!实际上我认为我无法记录所有设备,也无法在调试器中看到数据,即使我的记录被返回!