demo下载 http://download.csdn.net/detail/xlsn0w/9429187
写一个分类 在.h文件里面定义属性
#import <Foundation/Foundation.h>
@interface NSObject (Name)
@property (nonatomic,strong) NSString *name;
@end
在.m里面重写set get方法
#import "NSObject+Name.h"
#import <objc/objc-runtime.h>
@implementation NSObject (Name)
const void *key;
- (void)setName:(NSString *)name {
objc_setAssociatedObject(self,key, name, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)name {
returnobjc_getAssociatedObject(self,key);
}
然后在
ViewController里面就能通过点语法 点出name属性
#import "NSObject+Name.h"
#import <objc/objc-runtime.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.name =@"runtime使ViewController具有name属性";
NSLog(@"%@",self.name);
}
@end