iOS为类别添加属性的方法(RunTime)
2016-04-15 14:35
489 查看
一般认为Category不能添加变量,其实系统已经告诉我们是可以的.
这家伙已经给UIViewController添加了图中的几个属性,那么如何实现?
其实是使用@dynamic 来动态添加的。
(即运行时Runtime)
代码:
1.创建Person类
#import <Foundation/Foundation.h>
@interface Person :NSObject
@property (nonatomic,copy)NSString * name;
@end
2.创建Person的类别
#import "Person.h"
// 添加额外两个属性
@interface Person (addProperty)
@property (nonatomic,assign)NSInteger age;
@property (nonatomic,copy)NSString * stu;
@end
3.Person类别.m的实现
#import "Person+addProperty.h"
#import <objc/runtime.h>
@implementation Person (addProperty)
staticchar nameKey =
'n';
staticchar stuKey =
's';
// 给age属性提供getter和setter方法
- (NSInteger)age{
return [objc_getAssociatedObject(self, &nameKey)integerValue];
}
- (void)setAge:(NSInteger)age {
NSString * s = [NSStringstringWithFormat:@"%ld",(long)age];
objc_setAssociatedObject(self, &nameKey,s,OBJC_ASSOCIATION_COPY_NONATOMIC);
}
// 给stu属性提供getter和setter方法
- (NSString*)stu{
returnobjc_getAssociatedObject(self, &stuKey);
}
- (void)setStu:(NSString *)stu{
objc_setAssociatedObject(self, &stuKey, stu,OBJC_ASSOCIATION_COPY_NONATOMIC);
}
@end
4.用一下吧
#import "ViewController.h"
#import "Person+addProperty.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
Person * p = [[Personalloc]
init];
p.name =@"原有属性";
p.stu =@"添加的属性";
p.age =17;
NSLog(@"%@ %@ %ld",p.name,p.stu,p.age);
}
这家伙已经给UIViewController添加了图中的几个属性,那么如何实现?
其实是使用@dynamic 来动态添加的。
(即运行时Runtime)
代码:
1.创建Person类
#import <Foundation/Foundation.h>
@interface Person :NSObject
@property (nonatomic,copy)NSString * name;
@end
2.创建Person的类别
#import "Person.h"
// 添加额外两个属性
@interface Person (addProperty)
@property (nonatomic,assign)NSInteger age;
@property (nonatomic,copy)NSString * stu;
@end
3.Person类别.m的实现
#import "Person+addProperty.h"
#import <objc/runtime.h>
@implementation Person (addProperty)
staticchar nameKey =
'n';
staticchar stuKey =
's';
// 给age属性提供getter和setter方法
- (NSInteger)age{
return [objc_getAssociatedObject(self, &nameKey)integerValue];
}
- (void)setAge:(NSInteger)age {
NSString * s = [NSStringstringWithFormat:@"%ld",(long)age];
objc_setAssociatedObject(self, &nameKey,s,OBJC_ASSOCIATION_COPY_NONATOMIC);
}
// 给stu属性提供getter和setter方法
- (NSString*)stu{
returnobjc_getAssociatedObject(self, &stuKey);
}
- (void)setStu:(NSString *)stu{
objc_setAssociatedObject(self, &stuKey, stu,OBJC_ASSOCIATION_COPY_NONATOMIC);
}
@end
4.用一下吧
#import "ViewController.h"
#import "Person+addProperty.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
Person * p = [[Personalloc]
init];
p.name =@"原有属性";
p.stu =@"添加的属性";
p.age =17;
NSLog(@"%@ %@ %ld",p.name,p.stu,p.age);
}
相关文章推荐
- iOS 转换坐标系
- IOS学习之——界面2传值到界面1(代理方法)
- iOS开发系列--数据存取
- ios 之控制器的loadview
- iOS 上传照片到服务器 获取相册和拍照功能的照片 照片压缩上传
- GCD的一些原理
- iOS 获取系统图片
- iOS-上拉刷新,下拉加载-----------详解
- iOS崩溃crash大解析
- iOS 在tableView或scrollView上,消退键盘
- iOS开发之WMPageController的使用
- iOS RunLoop笔记
- iOS开发 引入支付宝 缺少 #include <openssl/asn1.h>
- iOS 版本更新迭代
- iOS开发中的一些图片上传压缩处理
- iOS 相关路径
- iOS常用数学常量宏
- iOS常用的数学函数
- 【iOS学习笔记 16-4-15】正则表达式基础
- ios / % 四舍五入 向上取整(ceil()) 向下取整(floor())