iOS runtime机制实现给分类添加属性
来源:互联网 发布:监听rpc服务器端口 编辑:程序博客网 时间:2024/04/30 21:59
一.创建分类并添加属性:
分类中声明的属性并未自动生成get和set方法,这里通过runtime机制动态实现get和set方法。
1.创建.h文件:
#import <UIKit/UIKit.h>typedef NS_ENUM(NSInteger,ReloadImageWay){ ReloadImageWayOne, ReloadImageWayTwo,};@interface UITableViewCell (reloadImage)@property (assign, nonatomic) ReloadImageWay cellReloadWay;-(void)loadImage;-(void)loadImageWayOne;@end
2.创建.m文件:
#import "UITableViewCell+reloadImage.h"#import <objc/runtime.h>static char *Key = "ReloadWay";@implementation UITableViewCell (reloadImage)-(ReloadImageWay)cellReloadWay{ ReloadImageWay way = [(NSString *)objc_getAssociatedObject(self, Key) intValue]; return way;}-(void)setCellReloadWay:(ReloadImageWay)reloadImageWay{ [self willChangeValueForKey:@"cellReloadWay"]; objc_setAssociatedObject(self, Key, [NSString stringWithFormat:@"%d",reloadImageWay], OBJC_ASSOCIATION_COPY); [self didChangeValueForKey:@"cellReloadWay"];}@end
二.调用分类中添加的属性
在用到属性的类里添加分类头文件,然后像正常添加的属性一样使用就ok了。
0 0
- iOS runtime机制实现给分类添加属性
- RunTime应用--动态给分类添加属性
- IOS通过runtime给category添加属性
- iOS runtime 机制 通过别扩展category给一个类添加属性
- iOS分类中通过runtime添加动态属性
- iOS分类中通过runtime添加动态属性
- iOS分类中通过runtime添加动态属性
- iOS分类中通过runtime添加动态属性
- iOS分类中通过runtime添加动态属性
- Runtime之分类添加属性
- Runtime消息机制,交换方法,动态添加方法,分类添加属性
- iOS给分类添加属性时使用 @selector()原理详解
- iOS-利用运行时给分类添加属性
- iOS利用运行时给分类添加属性
- iOS在分类中添加属性,运行时编程,objc runtime 动态增加属性
- 利用runtime给类别添加属性
- 给C++添加属性机制
- iOS为分类添加属性
- Android开发中网络编程与常见功能优化总结
- 二项队列(类结构及节点定义)
- 经典排序算法之冒泡排序
- MC9S12G128 内部flash与eeprom的读写配置
- Android-SDK-Windows的sdk manager闪退
- iOS runtime机制实现给分类添加属性
- 杭电2501
- iOS 自定义结构体、结构体转换成对象
- unix Domain socket(python)
- MC9S12G中文教学
- c++ virtual的作用
- TCP与UDP区别
- js exploit
- 蓝桥杯 欧拉和鸡蛋