如何向Swift3中的实体插入新数据?
在以前的swift中,我能够使用这样的代码向数据模型中的“TestEntity”添加新数据如何向Swift3中的实体插入新数据?,swift,core-data,swift3,nsmanagedobject,nsentitydescription,Swift,Core Data,Swift3,Nsmanagedobject,Nsentitydescription,在以前的swift中,我能够使用这样的代码向数据模型中的“TestEntity”添加新数据 NSManagedObject是为我的“TestEntity”创建的,我可以用“dot”语法设置它的属性 最后,我将保存上下文 let entity=NSEntityDescription.insertNewObject(forEntityName: "TestEntity", into: context) as! TestEntity entity.testAttribute="test value"
NSManagedObject
是为我的“TestEntity”创建的,我可以用“dot”语法设置它的属性
最后,我将保存上下文
let entity=NSEntityDescription.insertNewObject(forEntityName: "TestEntity", into: context) as! TestEntity
entity.testAttribute="test value"
context.save()
此代码在Swift3中不起作用。当我运行它时,会出现以下运行时错误:
无法强制转换类型为“NSManagedObject\u TestEntity”的值
(0x175306b0)到“testCoreData.TestEntity”(0xd6bb8)。2016-06-19
11:07:52.305195 testCoreData[689:264453]无法强制转换类型的值
“NSManagedObject\u TestEntity”(0x175306b0)至
'testCoreData.TestEntity'(0xd6bb8)
有谁能解释一下在swift3中应该如何做?非常感谢您的帮助。
多谢各位
问题的第二部分是如何再次访问数据。以下代码以错误结束:致命错误:NSArray
元素未能匹配Swift数组元素类型
let fr:NSFetchRequest<TestEntity>=TestEntity.fetchRequest()
do {
let searchResults=try context.fetch(fr)
if let results=searchResults {
for result in results {
print("testAtt = \(result.testAtt)")
}
}
} catch {
}
let fr:NSFetchRequest=TestEntity.fetchRequest()
做{
让searchResults=try context.fetch(fr)
如果let results=searchResults{
为了得到结果{
打印(“testAtt=\(result.testAtt)”)
}
}
}抓住{
}
如果存在NSManagedObject
子类TestEntity
则新语法为
let entity = TestEntity(context: context)
entity.testAttribute="test value"
工作示例:
let fr:NSFetchRequest<TestEntity>=TestEntity.fetchRequest()
do {
let results=try self.moc!.fetch(fr)
if results.count==0 {
print("no resutls. i need to add something")
let newTestEntity=TestEntity(context: self.moc!)
newTestEntity.testAtt="testovaci text"
do {
try self.moc!.save()
}catch{
}
}else{
print("already some results")
for result in results {
print(result.testAtt!)
}
}
}catch {
print(error)
}
let fr:NSFetchRequest=TestEntity.fetchRequest()
做{
让结果=尝试self.moc!.fetch(fr)
如果results.count==0{
打印(“没有结果,我需要添加一些内容”)
让newTestEntity=TestEntity(上下文:self.moc!)
newTestEntity.testAtt=“testovaci text”
做{
试试self.moc!.save()
}抓住{
}
}否则{
打印(“已有一些结果”)
为了得到结果{
打印(result.testAtt!)
}
}
}抓住{
打印(错误)
}
数据模型检查器TestEntity类名需要设置为TestEntity。以前的奇怪行为似乎是由于该值为空而导致的。我不是核心数据专家,但API提供了3种创建
NSManagedObject
实例的不同方法。我不知道他们中是否有人比其他人有任何好处
//retrieve the entity
let entity = NSEntityDescription.entity(forEntityName: "TestEntity", in: context)
let testEntity = NSManagedObject(entity: entity!, insertInto: context)
//set the entity values
testEntity.setValue(testAtt1, forKey: "testAtt1")
testEntity.setValue(testAtt2, forKey: "testAtt2")
//save the object
do {
try context.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
假设您有一个名为TestEntity
的实体/NSManagedObject子类,那么以下所有内容都是相同的:
新方式(无需铸造)+iOS10
:
let test = TestEntity(context: context)
test.attribute1 = "a value"
旧方法(如果您可以使用setValue
,则不需要强制转换。使用强制转换可以使用点表示法。+iOS3
let testEntity = NSEntityDescription.entity(forEntityName: "TestEntity", in: context)
let test1 = NSManagedObject(entity: testEntity!, insertInto: context)
test1.setValue("a value", forKey: "attribute1")
let test2 = NSManagedObject(entity: testEntity!, insertInto: context) as! TestEntity
test2.attribute1 = "a value"
或:
创建NSManagedObject实例并分配其属性后,只需将其保存在上下文中
do {
try context.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
保存必须被包装到
try-catch
块中。@Mundi是的,当然,但这与问题无关。我删除了行。我必须为上述代码创建NSEntityDescription才能工作。如果没有它,我会遇到另一个运行时错误:由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因是:“cl的NSManagedObject”ass“testCoreData.TestEntity”必须具有有效的NSEntityDescription。“let entityDes=NSEntityDescription.entity(forEntityName:“TestEntity”,in:context)let entity=TestEntity(entity:entityDes!,insertInto:context)entity.testAtt=“测试属性”
但它成功了。谢谢。Swift 3对核心数据进行了根本性更改。例如,有一个新类NSPersistentContainer
来管理核心数据堆栈的创建。创建一个新项目,启用核心数据以查看更改。无法为实体“EntityN”加载名为“ProjectName.EntityName”的类找不到ame'.Class,改为使用默认NSManagedObject。
do {
try context.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}