Created
January 2, 2014 10:14
-
-
Save ashikahmad/8217276 to your computer and use it in GitHub Desktop.
Add property in a category Collected from http://inchoo.net/mobile-development/iphone-development/how-to-add-a-property-via-class-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
// | |
// category_file.h | |
@interface SomeClass (Private) | |
@property (nonatomic, assign) id newProperty; | |
@end | |
NSString * const kNewPropertyKey = @"kNewPropertyKey"; | |
@implementation SomeClass (Private) | |
@dynamic newProperty; | |
- (void)setNewProperty:(id)aObject | |
{ | |
objc_setAssociatedObject(self, kNewPropertyKey, aObject, OBJC_ASSOCIATION_ASSIGN); | |
} | |
- (id)newProperty | |
{ | |
return objc_getAssociatedObject(self, kNewPropertyKey); | |
} | |
@end |
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
// category_file.m | |
@interface MyClass : NSObject | |
@property (retain, readonly) float value; | |
@end | |
// Private extension, typically hidden in the main implementation file. | |
@interface MyClass () | |
@property (retain, readwrite) float value; | |
@end | |
@interface MyClass : NSObject | |
- (float)value; | |
@end | |
@interface MyClass () { | |
float value; | |
} | |
- (void)setValue:(float)newValue; | |
@end | |
@implementation MyClass | |
- (float)value { | |
return value; | |
} | |
- (void)setValue:(float)newValue { | |
value = newValue; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment