Created
May 15, 2014 22:49
-
-
Save dlongmuir/bbc6d3b93bf936131321 to your computer and use it in GitHub Desktop.
Testing different ways of defining Objective-C variables
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 VariableNoBrackets : NSObject | |
@end | |
@implementation VariableNoBrackets | |
NSString *aString; | |
- (instancetype)initWithString: (NSString *)newString { | |
self = [super init]; | |
if (self) { | |
aString = newString; | |
} | |
return self; | |
} | |
- (NSString *)description { | |
return [NSString stringWithFormat:@"VariableNoBrackets string: %@ stringInstance %p", aString, aString]; | |
} | |
@end | |
@interface VariableWithBrackets : NSObject | |
@end | |
@implementation VariableWithBrackets { | |
NSString *_anotherString; | |
} | |
- (instancetype)initWithString: (NSString *)anotherString { | |
self = [super init]; | |
if (self) { | |
_anotherString = anotherString; | |
} | |
return self; | |
} | |
- (NSString *)description { | |
return [NSString stringWithFormat:@"VariableWithBrackets string: %@ stringInstance %p", _anotherString, _anotherString]; | |
} | |
@end | |
@interface VariableAccess : NSObject | |
@end | |
@implementation VariableAccess | |
- (NSString *)description { | |
return [NSString stringWithFormat:@"VariableAccess string: %@ stringInstance %p", aString, aString]; | |
} | |
@end | |
@interface UsingProperty : NSObject | |
@property NSString *propertyString; | |
@end | |
@implementation UsingProperty | |
- (instancetype)initWithString: (NSString *)aPropertyString { | |
self = [super init]; | |
if (self) { | |
self.propertyString = aPropertyString; | |
} | |
return self; | |
} | |
- (NSString *)description { | |
return [NSString stringWithFormat:@"PropertyString string: %@ stringInstance %p", self.propertyString, self.propertyString]; | |
} | |
@end | |
Test: | |
VariableNoBrackets *noBrackets1 = [[VariableNoBrackets alloc] initWithString: @"abc"]; | |
VariableNoBrackets *noBrackets2 = [[VariableNoBrackets alloc] initWithString: @"def"]; | |
NSLog(@"no brackets %@", noBrackets1); | |
NSLog(@"no brackets %@", noBrackets2); | |
VariableWithBrackets *withBrackets1 = [[VariableWithBrackets alloc] initWithString: @"123"]; | |
VariableWithBrackets *withBrackets2 = [[VariableWithBrackets alloc] initWithString: @"456"]; | |
NSLog(@"with brackets %@", withBrackets1); | |
NSLog(@"with brackets %@", withBrackets2); | |
VariableAccess *access1 = [[VariableAccess alloc] init]; | |
VariableAccess *access2 = [[VariableAccess alloc] init]; | |
NSLog(@"with access %@", access1); | |
NSLog(@"with access %@", access2); | |
UsingProperty *prop1 = [[UsingProperty alloc] initWithString: @"ABCDEF"]; | |
UsingProperty *prop2 = [[UsingProperty alloc] initWithString: @"DEFGHI"]; | |
NSLog(@"with property %@", prop1); | |
NSLog(@"with property %@", prop2); | |
Results: | |
2014-05-15 18:45:53.158 DerekSpike[5485:60b] no brackets VariableNoBrackets string: def stringInstance 0x6c91c // static | |
2014-05-15 18:45:53.160 DerekSpike[5485:60b] no brackets VariableNoBrackets string: def stringInstance 0x6c91c // static | |
2014-05-15 18:45:53.161 DerekSpike[5485:60b] with brackets VariableWithBrackets string: 123 stringInstance 0x6c93c // instance | |
2014-05-15 18:45:53.162 DerekSpike[5485:60b] with brackets VariableWithBrackets string: 456 stringInstance 0x6c94c // instance | |
2014-05-15 18:45:53.163 DerekSpike[5485:60b] with access VariableAccess string: def stringInstance 0x6c91c // static, global (at least within the file) | |
2014-05-15 18:45:53.164 DerekSpike[5485:60b] with access VariableAccess string: def stringInstance 0x6c91c // static, global | |
2014-05-15 18:45:53.165 DerekSpike[5485:60b] with property PropertyString string: ABCDEF stringInstance 0x6c97c // instance | |
2014-05-15 18:45:53.167 DerekSpike[5485:60b] with property PropertyString string: DEFGHI stringInstance 0x6c98c // instance | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment