readonly & readwrite
配合使用,一般在.h文件中如下定义
@interface XXXXX
@property (readonly, nonatomic, assign) int age;
@end
而在.m中:
@interface XXXXX()
@property (readwrite, nonatomic, assign) int age;
@end
@implementation XXXXX
@end
这样就可以在.m中使用
self.age = 1;
这样的语句了。
类属性
关键词: class
.h文件中
@interface RXAFNTest3Object : NSObject
@property (nonatomic, assign, class) NSInteger value;
@end
.m文件中
static NSInteger s_Value = 0;
@implementation RXAFNTest3Object
+ (NSInteger)value {
return s_Value;
}
+ (void)setValue:(NSInteger)value {
s_Value = value;
}
@end
在分类中添加属性
@interface UIViewController (Mediator)
@property (nonatomic, copy) NSString *rx_string;
@end
- (NSString *)rx_string {
return objc_getAssociatedObject(self, @"rx_string");
}
- (void)setRx_string:(NSString *)rx_string {
objc_setAssociatedObject(self, @"rx_string", rx_string, OBJC_ASSOCIATION_COPY);
}
在分类中添加weak属性
http://mrpeak.cn/blog/ios-weak/
- (void)setContext:(CDDContext*)object {
id __weak weakObject = object;
id (^block)() = ^{ return weakObject; };
objc_setAssociatedObject(self, @selector(context), block, OBJC_ASSOCIATION_COPY);
}
- (CDDContext*)context {
id (^block)() = objc_getAssociatedObject(self, @selector(context));
id curContext = (block ? block() : nil);
return curContext;
}
网友评论