Last active
January 3, 2016 23:29
-
-
Save trojanfoe/8535318 to your computer and use it in GitHub Desktop.
Demonstrate call-by-ref. See: http://stackoverflow.com/questions/21226504/using-delegation-vs-property-reverse-scope?noredirect=1#comment31997063_21226504
This file contains 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
#import <Foundation/Foundation.h> | |
@interface ClassA : NSObject | |
@property NSMutableString *string; | |
- (void)changeString; | |
- (void)printString; | |
@end | |
@implementation ClassA | |
- (void)changeString { | |
[self.string appendString:@"ClassA Did This"]; | |
} | |
- (void)printString { | |
NSLog(@"ClassA string='%@'", self.string); | |
} | |
@end | |
@interface ClassB : NSObject | |
@property NSMutableString *string; | |
- (void)changeString; | |
- (void)printString; | |
@end | |
@implementation ClassB | |
- (void)changeString { | |
[self.string appendString:@" ClassB Did This"]; | |
} | |
- (void)printString { | |
NSLog(@"ClassB string='%@'", self.string); | |
} | |
@end | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
ClassA *a = [[ClassA alloc] init]; | |
ClassB *b = [[ClassB alloc] init]; | |
NSMutableString *string = [[NSMutableString alloc] init]; | |
a.string = string; | |
b.string = string; | |
[a changeString]; | |
[a printString]; | |
[b printString]; | |
[b changeString]; | |
[a printString]; | |
[b printString]; | |
} | |
return 0; | |
} | |
This file contains 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
2014-01-21 06:33:00.798 CallByRef[37837:303] ClassA string='ClassA Did This' | |
2014-01-21 06:33:00.801 CallByRef[37837:303] ClassB string='ClassA Did This' | |
2014-01-21 06:33:00.802 CallByRef[37837:303] ClassA string='ClassA Did This ClassB Did This' | |
2014-01-21 06:33:00.802 CallByRef[37837:303] ClassB string='ClassA Did This ClassB Did This' | |
Program ended with exit code: 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment