简体   繁体   English

在Xcode 7.3中使用Swift实例化核心数据托管对象

[英]Instantiating Core Data Managed Objects Using Swift in Xcode 7.3

I am encountering a weird error when using Core Data with Swift in Xcode 7.3. 在Xcode 7.3中使用带有Swift的Core Data时遇到一个奇怪的错误。

I recently updated our codebase from Swift 1.0 to Swift 2.2 and am now getting an error when trying to access certain elements of an NSManagedObject extension. 我最近将代码库从Swift 1.0更新为Swift 2.2,现在尝试访问NSManagedObject扩展的某些元素时遇到错误。

I've set up the entity (called Member here) in the data model as shown below, with the class name and module matching what I've seen in other posts: 我已经在数据模型中设置了实体(这里称为Member),如下所示,其类名和模块与我在其他帖子中看到的相匹配:

Screenshot of Inspector View of Member Entity 成员实体的检查器视图的屏幕快照

The class itself has the following top most declaration: 该类本身具有以下最上面的声明:

@objc(Member) class Member: NSManagedObject { @objc(Member)类成员:NSManagedObject {

And I've tried both removing and adding the @objc part at the top as it's something I've seen people mention might cause issues. 我尝试过在顶部删除和添加@objc部分,因为这是我看到的人提到的可能会引起问题的部分。

I instantiate an instance of my NSManagedObjects through a convenicnece initializer in an extension of NSManagedObject as shown below: 我通过NSManagedObject扩展中的常规初始化程序实例化了NSManagedObjects的实例,如下所示:

convenience init(context: NSManagedObjectContext, className: String) {
    let entity = NSEntityDescription.entityForName(className, inManagedObjectContext: context)
    self.init(entity: entity!, insertIntoManagedObjectContext: context)
}

Finally the error occurs when I try to set certain properties on the Member object. 最后,当我尝试在Member对象上设置某些属性时,就会发生错误。 I thought it might have to do with whether the properties were optional or not but switching those around didn't seem to have an effect. 我认为这可能与属性是否可选有关,但是切换这些属性似乎没有效果。

The error I get is an EXEC_BAD_ACCESS error, but it seems to stem from the following thing that gets printed out when I instantiate the Member object: 我收到的错误是EXEC_BAD_ACCESS错误,但是它似乎是由于实例化Member对象时打印出的以下内容引起的:

MyAppName[23325:891360] CoreData: warning: Unable to load class named 
'MyAppName.Optional("Member")' for entity 'Member'.  Class not found, 
using default NSManagedObject instead.

Any insight would be appreciated as I am unsure of what else to try, as well as why it's adding ".Optional" to the start of the class name. 由于我不确定要尝试其他方法以及为什么要在类名的开头添加“ .Optional”,因此我们将不胜感激。

Thank you. 谢谢。

Don't force unwrap the optional you get back from NSEntityDescription.entityForName . 不要强迫解开从NSEntityDescription.entityForName返回的可NSEntityDescription.entityForName You should probably put a guard let on the entity and throw if the entity does not return. 您可能应该在实体上放一个防护套,如果实体不返回则抛出该保护套。

As an aside, I doubt this convenience method buys you anything over just calling NSEntityDescription.insertNewObjectForEntityForName(_:inManagedObjectContext:) . NSEntityDescription.insertNewObjectForEntityForName(_:inManagedObjectContext:) ,我怀疑这种方便的方法是否仅通过调用NSEntityDescription.insertNewObjectForEntityForName(_:inManagedObjectContext:)买到任何东西。 You are still dealing with a magic string and the context. 您仍在处理魔术字符串和上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM