Key value coding (KVC)
The Core Data uses KVC to store and retrieve data from its managed objects. NSManagedObject
supports the key value methods valueForKey:
and setValue:forKey:
for setting and retrieving attribute values from the managed object respectively.
Key value methods
The two key value methods that are widely used for setting and retrieving values of the attributes of the managed object are explained in the following sections.
The -valueForKey: method
The method -valueForKey:
is a generic accessor to retrieve the specified attribute value from a managed object.
For example, let's say we have a managed object (Customer) and it has an attribute called name
; we can obtain the value of the name attribute with the following:
NSString *custName = [managedObject valueForKey:@"name" ];
The value of the attribute name
is retrieved from the managed object and is assigned to the NSString instance custName
.
Similarly, the code used in the cellForRowAtIndexPath
method in the RootViewController...