Skip to content

Instantly share code, notes, and snippets.

@valexa
Created February 12, 2012 14:29
Show Gist options
  • Save valexa/1808794 to your computer and use it in GitHub Desktop.
Save valexa/1808794 to your computer and use it in GitHub Desktop.
self accessor gotcha
@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