I have a simple NSObject
subclass with some properties
@interface MyThing : NSObject
@property (nonatomic, copy) NSString *aString;
//... and so on
@end
But when I try to use key/value coding to set my properties via a dictionary:
+ (instancetype)thingFromDictionary:(NSDictionary *)dict
{
MyThing *newThing = [MyThing new];
for (NSString *key in dict)
{
if ([newThing respondsToSelector:@selector(key)])
{
//do stuff
[newThing setValue:[dict objectForKey:key] forKey:key];
}
else
{
NSLog(@"key %@ doesnt exist. value %@", key, [dict objectForKey:key]);
}
}
return newThing;
}
It turns out that though the dictionary contains keys that match the exact names of my properties, respondsToSelector:
always returns for NO
for those keys. How do I ensure all properties are accessible via the key/value methods?