Why isn’t a setter synthesized for myString in the example below? The basic assignment below results in myString being nil. Trying to use the setter [self setMyString:s]; results in an unrecognized selector exception.
// in .h file
@interface MyClass
@property (nonatomic, readonly) NSString *myString;
@end
// in .m file
@interface MyClass (MyCategory)
@property (nonatomic, copy) NSString *myString;
@end
@implementation MyClass
@synthensize myString;
- (void) someMethod:(NSString *) s {
myString = [s copy];
// why is myString nil here?
}
@end
Edit: the problem was with gdb. po myString printed Can't print description of a NIL object.. However NSLog(@"myString: %@", myString); printed the expected value.
The other two answers are correct, but I think they miss your intention. It’s common to declare a property as read-only in the .h file, so that code outside the class implementation can’t write it. Inside the .m file, you want it to be readwrite. This kind of redefinition is explicitly supported. However, you need to put the redeclaration as readwrite in a class-extension:
You do still need to use
self.myString = aStringor[self setMyString:aString], instead of writing to the ivar directly as you’re doing right now.