Created
March 14, 2013 21:42
-
-
Save armadsen/5165525 to your computer and use it in GitHub Desktop.
Quick and dirty illustration of @Property declared (but not synthesized) in a category.
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 MyClass : NSObject | |
@end | |
@implementation MyClass | |
@end | |
@interface MyClass (Properties) | |
@property (nonatomic, copy) NSString *string; | |
@end | |
@implementation MyClass (Properties) | |
// @synthesize string = _string; // Won't work | |
- (NSString *)string | |
{ | |
NSString *result = @"It's a string!"; // Get string from somewhere (derived from other properties, associated object, etc.) | |
return result; | |
} | |
- (void)setString:(NSString *)string | |
{ | |
// Set string somehow (set other properties derived from this value, create associated object, etc.) | |
// NSLog to show that this will be called | |
NSLog(@"Got a string: %@", string); | |
} | |
@end | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
MyClass *object = [[MyClass alloc] init]; | |
NSLog(@"object.string: %@", object.string); | |
object.string = @"Hello"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment