定义属性
@property (nonatomic, assign) NSInteger unreadCount;
使用运行时关联对象
- (NSInteger)unreadCount {
return [objc_getAssociatedObject(self, @selector(unreadCount)) intValue]; <span style="color:#ff0000;">//运行至此处报错</span>
}
- (void)setUnreadCount:(NSInteger)unreadCount {
objc_setAssociatedObject(self, @selector(unreadCount), @(unreadCount), OBJC_ASSOCIATION_ASSIGN);
}
出现以下报错:
-[NSConcreteValue intValue]: unrecognized selector sent to instance 0x16604de0
libc++abi.dylib: terminate_handler unexpectedly threw an exception
解决方法:
将 OBJC_ASSOCIATION_ASSIGN 改成 OBJC_ASSOCIATION_RETAIN 即可