I've read Subclassing NSManagedObject with swift 3 and Xcode 8 beta and read this great tutorial. Still have questions on some points.
The similarities are:
- I can customize both classes however I like.
- I can add new attributes or remove or rename attributes. ie for
category/extension
it will get updated upon a new build (in the derived data), and in case of manual/none
it will leave the class file intact and update the extension in the file navigation ie I won't end up with a duplicate file. This is all handled by Xcode because they are marked with a preprocessor @NSManaged
- Dumping something like
@NSManaged public var name: String?
straight into an existing NSManagedObject
subclass is not allowed. I tried to do entity.name = "John"
but I got the following error: reason: '-[SomeEntity setName:]: unrecognized selector sent to instance 0x60400009b120'
. I believe that's reasonable. I think without using the Core Data Model Editor the setter/getter accessor methods are not created.
The differences are:
- For
Category/Extension
you just need to create the class yourself and add any extra functions/properties you need.
For Category/Extension
the attributes are created in derived data which is enough. Because you never need to see that file. Its existence is enough to get things working.
?And specifically in the context of making changes to your NSManaged properties:
Changing property type, e.g. NSDate
to Date
is allowed only for Manual/None
. Example here
- Changing optionality of a type, e.g.
String?
to String
is allowed only for Manual/None
. Example here
Changing a property access level, e.g. from public
to private
is allowed only for Manual/None
. Example here
Having that said there is significant difference if I choose Manual/None
codegen and but don't select 'create NSManagedObject subclass'. In that case I have start writing all the code myself (subclass from NSManagedObject and write NSManaged for every property)...or if I don't write all that code myself then I can still access/set fields using KVC which is awkward!
In a nutshell I'm just trying to figure out the full extent of capabilities that I can get from using Manual/None
.
Question: Aside from the 9 notes which I need to know if I have validated correctly, an important question would be: how does me changing NSDate
to Date
or optional to non-optional not break the mappings between my NSManagedObject class and my object graph all while changing an NSDate
property to String
does break!! Does this have something to do with things that have guaranteed casting between Swift and Objective-C ie things that can be casted through as
— without ?
or !
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…