Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#typedef and KVC in ObjC

I have a class that looks like this:

@interface Properties : NSObject {
@private
    NSNumber* prop1;
    NSNumberBool* prop2;
    //etc

where NSNumberBool is a typedef:

// in MyApp_Prefix.pch
typedef NSNumber NSNumberBool;

I have all the required @property and @synthesize declarations to make prop1 and prop2 properties.

Everything compiled and worked fine until I tried to access prop2 by [myProperties valueForKey:@"prop2"]. This gives me a "class is not key-value compliant" error. However, many similar calls work fine:

myProperties.prop2; //works
[myProperties prop2]; //works
[myProperties valueForKey:@"prop1"]; //works
[myProperties valueForKey:@"prop2"] // throws NSUnknownKeyException ??

What is going on here, and how can I fix it?

Thanks,

like image 243
tba Avatar asked Oct 16 '09 07:10

tba


1 Answers

I suspect this is an issue with the way typedef interacts with the encode method.

I believe that typedef remains a pure C keyword and really only happens to work with Objective-C "types" usually because they happen to be implemented as structs.

As a result when you typedef NSNumber to NSNumberBool it works fine for method calls (and dot syntax properties) but (assuming my theory is correct), breaks encode which can't tell that NSNumberBool and NSNumber are the same type.

I'll be interested to see what someone who knows better says.

like image 146
philsquared Avatar answered Nov 24 '22 03:11

philsquared