I am time implementing the key value coding in my program so i am not much comfortable with it . I am having the controller class in which the parsing methods are implemented in start document i have initialize the object and keys. All data members of class Universal are type id.
univ=[[Universal alloc] init];
[univ setValue:univ.datamember1 forKey:@"1"];
[univ setValue:univ.datamember2 forKey:@"2"];
[univ setValue:univ.datamember3 forKey:@"3"];
[univ setValue:univ.datamember4 forKey:@"4"];
[univ setValue:univ.datamember5 forKey:@"5"];
I am setting the value on data member in end element method :
id temp1=[univ valueForKey:@"1"];
temp1=strVal;
[resultArray addObject:univ];
and i am getting the following exception for this code:
'NSUnknownKeyException', reason: '[<Universal 0x6e994d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key 1
what is the reason for this. Is there missing something or everything gone wrong??
For the most part, you don’t have to implement Key-Value Coding. Instead, you create your classes to comply with various conventions and then Key-Value Coding works for it automatically.
The conventions are about how you name methods in order to make a property accessible via KVC.
Your code:
is strange. What are you trying to do here? This code looks like it’s trying to set properties on the
univobject from other properties of theunivobject. That would be redundant.I suspect you think that you’re establishing synonyms or aliases here – that you’re making key “1” map to the property
datamember1. That’s not correct. The-setValue:forKey:method is for actually setting a property’s value. It’s very much like calling a setter method; in fact, it usually does result in calling a setter method.So, what you’ve written is very much like the following:
I doubt that’s anything that you meant to do. It might help you to explain what you’re trying to do in non-KVC terms. What properties are you trying change (if you are)? What values would you like to assign to them? How would you do that using just ordinary setters and getters?
Later, you posted this code:
Again, this is strange and confusing. I suspect you’re trying to change a property value on
univ, but that’s not what that code accomplishes, even if it could be made to work. The first line attempts to get the value of a property of theunivobject, where the property’s name is “1”. That’s not a valid name for a property. Anyway, it stores the retrieved value into a local variabletemp1. The second line simply throws away the result of the first line and stores a different value into the localtemp1variable. Thetemp1variable is independent of theunivobject, even though a moment ago it was storing a result retrieved from theunivobject. Changingtemp1can’t changeuniv(although messaging the object pointed to bytemp1could change that object and it might also be pointed to byuniv).It seems to me that you aren’t yet ready to be using Key-Value Coding. You need a better understanding of the basics. Also, it’s almost never necessary to use Key-Value Coding with a static, known-at-compile-time key. Key-Value Coding is for dynamic access to properties of an object when you don’t know the name of that property at compile time but you’ll have the name as data at run time.