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:

  1. It magically replaces key value coding.
  2. It is roughly equivalent to @dynamic in Objective-C (which I don't know much about)
  3. I need it to subclass PFObject from the Parse SDK. It normally uses KVC to read/write values from/to the backend.
  4. 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:

@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.


Solution 1:

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

Solution 2:

In the apple docs, for Custom Managed Object Class, they quote properties example like... enter image description here To me it seems there is no difference, I have used @dynamic in objective C, it seems @NSManaged is the replacement in Swift.