This is probably pilot error on my part, but I am a little confused why this does not return an int (as thats the type of the property identified by the key path). Does valueForKeyPath: return an object instead, can anyone explain.
// Simple Object
@interface Hopper : NSObject
@property(nonatomic, assign) int mass;
@end
// Test
Hopper *hopper = [[Hopper alloc] init];
[hopper setMass:67];
NSLog(@"HOPPER: %d", [hopper valueForKeyPath:@"mass"]);
.
WARNING: Conversion specifies type 'int' but the argument has type 'id'
Yes, it returns an objc object:
- (id)valueForKeyPath:(NSString *)keyPath;
Details for automatic conversions from non-objc objects to objc objects (e.g. NSNumber
and NSValue
) is covered in Accessor Search Patterns for Simple Attributes.
Therefore, you would use the objc object format specifier %@
:
NSLog(@"HOPPER: %@", [hopper valueForKeyPath:@"mass"]);