What does @NSManaged do?
IosObjective CSwiftCocoa TouchParse PlatformIos Problem Overview
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:
- It magically replaces key value coding.
- It is roughly equivalent to
@dynamic
in Objective-C (which I don't know much about) - I need it to subclass
PFObject
from theParse SDK
. It normally uses KVC to read/write values from/to the backend. - Prefixing any variable with
@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:
https://stackoverflow.com/questions/1160498/synthesize-vs-dynamic-what-are-the-differences
https://stackoverflow.com/questions/4524954/what-is-common-case-for-dynamic-usage/4524983#4524983
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.
Ios Solutions
Solution 1 - Ios
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<br/> here someone even used @NSManaged without CD because he wanted the @dynamic behaviour
Solution 2 - Ios
In the apple docs, for Custom Managed Object Class, they quote properties example like...
To me it seems there is no difference, I have used @dynamic
in objective C, it seems @NSManaged
is the replacement in Swift.
Solution 3 - Ios
Above mentioned answers are right. Here is my understanding.
@NSManaged indicates that the variables will get some values when we run the app. Coredata automatically creates getter and setter for such props. It silences the compiler for warnings.
NSmanaged is subclass of NSObject. @NSManaged means extra code will be given to these props at runtime. It tracks the changes made to those properties.