Added member variables for iOS classes
RT
// Add a member variable # import
@ Interface NSObject (AddProperty) @ property (nonatomic, strong) NSString * stringProperty; @ property (nonatomic, assign) NSInteger integerProperty; @ end
# Import NSObject + AddProperty. h # import
// Objc_getAssociatedObject and objc_setAssociatedObject both need to specify a fixed address, which is used to represent the attribute key and act as a constant. Static const void * StringProperty = & StringProperty; static const void * IntegerProperty = & IntegerProperty; // static char IntegerProperty; @ implementation NSObject (AddProperty) @ dynamic stringProperty; // set-(void) setStringProperty :( NSString *) stringProperty {// use that a static const as the key objc_setAssociatedObject (self, StringProperty, stringProperty, OBJC_ASSOCIATION_RETAIN_NONATOMIC ); // use that property's selector as the key: // objc_setAssociatedObject (self, @ selector (stringProperty), stringProperty, OBJC_ASSOCIATION_RETAIN_NONATOMIC);} // get-(NSString *) stringProperty {return objc_getAssociatedObject (self, StringProperty);} // set-(void) setIntegerProperty :( NSInteger) integerProperty {NSNumber * number = [[NSNumber alloc] initWithInteger: integerProperty]; objc_setAssociatedObject (self, IntegerProperty, number, OBJC_ASSOCIATION_ASSIGN);} // get-(NSInteger) integerProperty {return [objc_getAssociatedObject (self, IntegerProperty) integerValue];} @ end
// Obtain the member variable list
@interface NSObject (Property)-(NSDictionary *)propertyDictionary;+ (NSArray *)classPropertyList;@end
# Import NSObject + Property. h # import
# Import
@ Implementation NSObject (Property)-(NSDictionary *) propertyDictionary {// create a variable dictionary NSMutableDictionary * dict = [NSMutableDictionary dictionary]; unsigned int outCount; objc_property_t * props = class_copyPropertyList ([self class], & outCount); for (int I = 0; I