Created
February 12, 2012 14:29
-
-
Save valexa/1808794 to your computer and use it in GitHub Desktop.
self accessor gotcha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface AppDelegate : NSObject <NSApplicationDelegate> { | |
NSMutableString *retainedProperty; //explicitly declaring ivar backing is only required for the 32 bit runtime, is automatically added by the compiler for 64 bit | |
} | |
@property (retain) NSMutableString *retainedProperty; | |
@end | |
@implementation AppDelegate | |
@synthesize retainedProperty; | |
-(void)awakeFromNib | |
{ | |
int testCase = 0; | |
if (testCase == 0){ | |
//retainedProperty is null | |
}else if (testCase == 1){ | |
retainedProperty = [NSMutableString stringWithString:@"foo"]; //sets the ivar, unretained | |
}else if (testCase == 2){ | |
self.retainedProperty = [NSMutableString stringWithString:@"foo"]; //sets the property, retained, equivalent with [self setRetainedProperty:[NSMutableString stringWithString:@"foo"]]; | |
} | |
NSLog(@"%@",retainedProperty); //in the same scope, case 0: null, case 1: foo, case 2: foo | |
} | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
NSLog(@"%@",retainedProperty); //in the same scope, case 0: null, case 1: [random object/crash], case 2: foo | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment