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.m file is as follows:

cell.textLabel.text = [[managedObject valueForKey:@"name"] description];

The valueForKey:@"name" retrieves the value of the attribute name of the managed object and assigns it to the table cell for display.

The -setValue:forKey: method

It dynamically sets the properties (attributes) on an object.

For example, to set the value of the name attribute of the managed object to the string John , we use following statement:

[managedObject setValue: @"John" forKey:@"name"];

The preceding statement will first search for the setName: method. If the method is not available, it will look for the name attribute and use it directly to set its value. If the name attribute is also not available, then the setValue:forUndefinedKey: method is called on the managed object.

That is, we can manipulate managed objects dynamically using KVC without writing their accessors or mutators.

Note

Using KVC, we can access all the properties of an NSManagedObject protocol.

Keypath

KVC also includes the concept of a keypath that helps in iterating through object hierarchies using a single string. For example, let's say there are two entities, Customer and Product, and their relationship is established from the Customer to Product entity by the name products. We want to access the attribute prodname of the Product entity from the Customer entity. We can do so by using a keypath as follows:

NSString *productname = [managedObject valueForKeyPath:@"products.prodname"];

We can see that the value of the keypath is @"products.prodname". When this value is parsed by KVC, it will be divided into two separate values, namely, products and prodname. The products relationship will return a managed object instance that represents the Customer's product, that is, product entity, and the second part of the prodname keypath will retrieve the value of the prodname attribute from the managed object that represents the product entity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.136.152