简体   繁体   English

如何在 Swift 5 中强制更新 CoreData 代码文件(属性扩展)?

[英]How to force update CoreData Codefiles (Property Extension) in Swift 5?

i am a bit confused with CoreData.我对 CoreData 有点困惑。 In my xcdatamodeld I am able to create new properties and change some things about them, like whether they are optional or not .在我的xcdatamodeld ,我能够创建新属性并更改有关它们的一些内容,例如它们是否是可选的

For example, I created a property for my entity and called it descrip , the type is a String and I checked the Optional Field .例如,我为我的实体创建了一个属性并将其命名为 descrip ,类型是String并且我检查了Optional Field

At some other point I try to create a new Object of this Entity and safe it to CoreData:在其他时候,我尝试创建该实体的新 Object 并将其安全地保存到 CoreData:

let newEntity = Entity(context: context)
newEntity.name = name
newEntity.descrip = descrip // descrip is an optional String
newEntity.filename = filename

I get the following error message: Value of optional type 'String?' must be unwrapped to a value of type 'String'我收到以下错误消息: Value of optional type 'String?' must be unwrapped to a value of type 'String' Value of optional type 'String?' must be unwrapped to a value of type 'String'

Then, I tried to create the NSManagedObjectSubclasses for my Entity.然后,我尝试为我的实体创建 NSManagedObjectSubclasses。 I discovered, that while all properties have been added and the type of all is set correctly, the?我发现,虽然所有属性都已添加并且所有的类型都设置正确,但是? indicating the Optionality of the property was messed up completely.表明该属性的可选性完全搞砸了。

import Foundation
import CoreData


extension Entity {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Entity> {
        return NSFetchRequest<Entity>(entityName: "Entity")
    }

    @NSManaged public var name: String? // Should not be optional and was set so in xcdatamodeld
    @NSManaged public var descrip: String // Should be optional and set to optional in xcdatamodeld
    @NSManaged public var filename: String
}

Is there any way to force update this code file?有没有办法强制更新这个代码文件? Or how can I use optional strings to fill out my new Entity's optional values?或者我如何使用可选字符串来填写我的新实体的可选值?

The error in that assignment is not a Core Data problem, it's pure Swift.该分配中的错误不是核心数据问题,而是纯粹的 Swift。 You've declared descrip as String , yet you are trying to assign a String?您已将descrip声明为String ,但您正在尝试分配一个String? value.价值。 That's not legal in Swift-- you must unwrap an optional before assigning it to a non-optional.这在 Swift 中是不合法的——你必须先解开一个可选项,然后再将它分配给非可选项。 It would be the same if you weren't using Core Data.如果您不使用 Core Data,情况也是如此。 For example this would produce the same error:例如,这将产生相同的错误:

let foo: String? = "hello"
let bar: String = foo

The differences between the data model and the generated code happen because Swift optionals and Core Data optionals are completely different things.数据 model 和生成的代码之间存在差异,因为 Swift 选项和 Core Data 选项是完全不同的东西。 Core Data only cares if something is optional when you save changes. Core Data 只关心保存更改时是否有可选内容。 Swift cares about whether something is optional all the time. Swift 始终关心某些东西是否是可选的。 It's different because Core Data was around for a long time before Swift existed, so it doesn't always match up with what Swift expects.这是不同的,因为 Core Data 在 Swift 存在之前已经存在了很长时间,所以它并不总是与 Swift 的预期相匹配。

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

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