Sean Danzeiser
Sean Danzeiser

Reputation: 9243

unrecognized selector when calling method defined in class extension

I've got a class where I'm defining a class extension within the header file like so:

@interface GCFriend : GCDataObject

@property (nonatomic, strong) NSString *firstName;
...
...
...    

+ (NSOperation *)getFriendsCached:(void (^)(NSArray *cached))cached
                            fresh:(void (^)(BOOL success, NSArray *fresh))fresh;    
@end

@interface GCFriend (Transient)

@property (nonatomic, strong) UIImage *image;

@end

Now, as a matter of preference i'd like to keep that image property separate from the main interface declaration as its not something that comes down from the api. However, when i declare it this way, I get an unrecognized selector when I call the getter method. Why is that? there is no issue if i move it up to the main interface declaration.

Upvotes: 1

Views: 695

Answers (2)

Nicolas
Nicolas

Reputation: 933

That's interesting. There really should be a warning. I guess it's because of implicit synthesis of properties but that cannot work on a property declared in a category.

Anyway, you can keep your header file as it is but you will have to code explicitly the property getter and setter in the .m file.

...
@interface GCFriend() {
    UIImage *image; // create the ivar 
}
@end

@implementation GCFriend (Transient) 

- (UIImage *) image {  // getter
    return image ;
}

- (void) setImage:(UIImage *) img { // setter
    image = img ;
}
@end

Upvotes: -1

Chuck
Chuck

Reputation: 237110

That is not a class extension. That is a category. A class extension is typically placed in the implementation file of the class and is of the form @interface GCFriend () — empty parentheses. You can add instance variables in class extensions, but not in categories. (This is because class extensions are compiled as part of the class, while categories are compiled and loaded separately.)

Upvotes: 6

Related Questions