I have code such as the following which doesn’t work unless the category name is left blank
PrivatePropertyTest.h
@interface PrivatePropertyTest : NSObject
@property (readonly) int readonly;
- (void) testMethod;
@end
PrivatePropertyTest.m
#import "PrivatePropertyTest.h"
@interface PrivatePropertyTest (/*If I place a name in here it doesn't work*/)
@property (readwrite) int readonly;
@end
@implementation PrivatePropertyTest
@synthesize readonly;
- (void) testMethod
{
self.readonly = 2;
}
@end
main.m
#import <Foundation/Foundation.h>
#import "PrivatePropertyTest.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
PrivatePropertyTest *pPT = [[PrivatePropertyTest alloc] init];
[pPT testMethod];
//pPT.readonly = 1;
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
When I give it a name is says the setter Method for the selector doesn’t exist. Does this have to do with name mangling? Why does it matter if I name it or not?
If you can declare unnamed categories like this, can more than one unnamed category be declared for the same class?
The problem is, is that isn’t an unnamed category. It’s a class extension.
Class extensions are a bit like categories in that you can declare methods and properties for the class to implement. But extensions are actually part of the main implementation of the class. This means you can do things like override property access behavior (what you are doing) or add ivars.
Class extensions are required to be compiled with the implementation block, and there can only be one of them.