Skip to content

Instantly share code, notes, and snippets.

@armadsen
Created March 14, 2013 21:42
Show Gist options
  • Save armadsen/5165525 to your computer and use it in GitHub Desktop.
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.
#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