I’d like to use properties for my instance variables, but in many cases, I only want the class itself to have access to the setter. I was hoping I could do something like this:
Foo.h:
@interface Foo {
NSString *bar;
}
@property (readonly) NSString *bar;
@end
Foo.m:
#import "Foo.h"
@interface Foo ()
@property (copy) NSString *bar;
@end
@implementation Foo
@synthesize bar;
@end
But this generates a warning:
Foo.m:4: warning: property ‘bar’ attribute in ‘Foo’ class continuation does not match class ‘Foo’ property
I can see what it’s complaining about, but it still seems like a useful idiom. Is there some other way to accomplish this without writing my own setters?
Your approach is correct, but the redeclaration of
@property barin the class extension must match the original declaration except for readwrite vs. readonly. So this will work:Foo.h
Foo.m:
(recall that the default is
assignfor properties, notcopy).