I have encountered this keyword in various occasions. I kind of know what it's suppose to do. But I really want a better understanding of it.
What I noticed about @NSManaged
- based not on documentation, but through repeated use:
@dynamic
in Objective-C (which I don't know much about)PFObject
from the Parse SDK
. It normally uses KVC to read/write values from/to the backend.@NSManaged
will shut the compiler up when I don't initialize within the initializer. The formal definition (in the Core Data Apple Docs):
Core Data provides the underlying storage and implementation of properties in subclasses of the NSManagedObject class. Add the @NSManaged attribute before each property definition in your managed object subclass that corresponds to an attribute or relationship in your Core Data model. Like the @dynamic attribute in Objective-C, the @NSManaged attribute informs the Swift compiler that the storage and implementation of a property will be provided at runtime. However, unlike @dynamic, the @NSManaged attribute is available only for Core Data support.
What I got from that:
Variables with
@NSManaged
shall be exempt from compile time checks for something.
I've read the formal documentation and various other SO questions regarding this matter:
@synthesize vs @dynamic, what are the differences?
What is common case for @dynamic usage?
I instinctively recognize some scenarios where I should use it. I partially know what it does. But what I seek is purer understanding of what it does.
Further Observations:
A PFObject
in the Parse SDK
relies on Key Value Coding
to access its values. The PFObject
provides the following accessors:
objectForKey:
let score = results.objectForKey("descriptionOfResult") //returns the descriptionOfResult value from the results object
setObject:forKey:
results.setObject("The results for a physics exam", forKey: "descriptionOfResult") //sets the value of descriptionOfResult
To my understanding, @NSManaged
magically understands that the variable I've declared automatically uses the above accessors to get
and set
. I'd like to know how it does that (if what I understand is true), and whatever else it does.
Core Data uses a schema called a managed object model — an instance of NSManagedObjectModel . In general, the richer the model, the better Core Data is able to support your application. A managed object model allows Core Data to map from records in a persistent store to managed objects that you use in your application.
From the Xcode menu bar, choose Editor > Create NSManagedObject Subclass. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both class and properties files into your project.
Using a transient property ensures that Core Data tracks the property such that the accessing the property will cause a fault to fire and provide the data.
A description of search criteria used to retrieve data from a persistent store.
Yes, it kinda really acts like @dynamic -- technically it might be identical even. Semantically there is a slight difference:
@dynamic says 'compiler, don't check if my properties are also implemented. There might be no code you can see but I guarantee it will work at runtime'
@NSManaged now says 'compiler, don't check those properties as I have Core Data to take care of the implementation - it will be there at runtime'
so you could even say: @NSManaged is syntactic sugar that is a more narrow version of dynamic :)
https://github.com/KyoheiG3/DynamicBlurView/issues/2
here someone even used @NSManaged without CD because he wanted the @dynamic behaviour
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With