3

I have an object which straight forward instance variables. Some are NSString, some are unsigned ints, etc. I was under the impression Key-Value coding was scriptable in that I could write code such as the following:

   id key, value;
   key      = [[attributes objectAtIndex:j] name];
   value    = [[attributes objectAtIndex:j] stringValue];
   [obj setValue:value forKey:key];

The reason I'm using stringValue here and name is I'm working with TouchXML and building objects from XML feeds.

The code above crashes if I try to assign anything to an integer. If I change my unsigned int to an instance of NSString, there isn't any run-time crashes. It was to my understanding that Key-Value coding is smart enough to detect these issue.

What are my possible solutions? How can I abstract this as much as possible so I can run the same method on many different classes which all have their own instance variables?

Is the most elegant method to really override my setter and use NSNumber to convert the string into a type int, which I thought was already handled by KVC?

4

1 回答 1

3

KVC doesn't do any type coercion/translation internally, so the type of the object you pass into setValue:forKey: must match the type of the instance variable it will be stored in. Native types such as int, long, etc. are packaged up into NSNumber objects, and things like structs get packed into NSValue objects. To set a value that's declared as a native type, you should pass an appropriate wrapper object in for the value argument.

I would also recommend doing any required translation in the code that's calling the setter, and not in the setter itself. That's more a matter of opinion though, so I suppose you could translate stuff in the setter if you really want to. My version of your code would look something like:

id key, value;
key = [[attributes objectAtIndex:j] name];
value = [NSNumber numberWithInt:[[[attributes objectAtIndex:j] stringValue] intValue]];
[obj setValue:value forKey:key];
于 2008-12-19T20:10:46.897 回答