Say I have the following:
@interface MyClass : NSObject { NSString* _foobar; }
@property (nonatomic, retain) NSString* foobar;
@end
@implementation MyClass
@dynamic foobar;
- (void) setFoobar:(NSString*)fbSet; { [_foobar release]; _foobar = [fbSet retain]; }
- (NSString*) foobar; { return _foobar; }
@end
Then:
MyClass* mcInst = [[[MyClass alloc] init] autorelease];
NSLog(@"I set 'foobar' to '%@'", (mcInst.foobar = @"BAZ!"));
Looking at the return value of -[MyClass setFoobar:]
, one might assume here that this line would print I set 'foobar' to ''
, because the assignment appears to return nothing.
However - thankfully - this assignment acts as expected, and the code prints I set 'foobar' to 'BAZ!'
. Unfortunately, this feels like a contradiction, because the invoked setter's return value belies the fact that the assignment returns the value assigned to it. At first I figured that mcInst.foobar = @"BAZ!";
is making two calls instead a block: first the setter and then the getter to gather the return value. However, instrumenting the setter and getter methods with NSLog
calls proves this isn't the case.
I'd like to understand the ambiguity here. Thanks!