Rollo Konig-Brock
Rollo Konig-Brock

Reputation: 70

NSXMLParser, the setting of values and keys

After the following of a tutorial I looked where the value and the corrosponding 'key' was set.

[aBook setValue:currentElementValue forKey:elementName];

My one problem with this is that this method is for dictionaries, not NSArrays.

Upvotes: 1

Views: 195

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

The setValue:forKey: method applies to everything that supports Key Value Coding. It lets you set a value for a property identified by a string. In this particular case, the string comes from the name of the XML element the parser has finished processing, and the value comes from the text of the corresponding element.

The KVC trick lets you use the XML parser to set values of objects without hardcoding the names of objects' properties or methods. For example, if you have a fragment of XML like this

<book>
    <author>Kernighan and Ritchie</author>
    <title>The C Programming Language</title>
</book>

and a KVC-compliant Book class with NSString* properties author and title, the parser would be able to process the XML and set values of the properties without knowing anything about the structure of your XML or the structure of your Book class.

Upvotes: 1

Related Questions