runtime之@dynamic属性
来源:互联网 发布:各种网络协议的区别 编辑:程序博客网 时间:2024/05/17 01:29
@dynamic属性用于通知编辑器不要生成属性对应的实例变量和setter getter 方法,但是在编译时期调用对应的存取方法不会报错,再次验证了OC是一门动态语言
#import <Foundation/Foundation.h>@interface AutoDictionary : NSObject@property (nonatomic, copy) NSString *string;@property (nonatomic, strong) NSNumber *number;@property (nonatomic, strong) NSDate *date;@end
#import "AutoDictionary.h"@interface AutoDictionary ()@property (nonatomic, strong) NSMutableDictionary *dic;@end@implementation AutoDictionary@dynamic string,date,number;@end
VC里面调用如下:
#import "AutoDictionary.h"@interface ViewController ()@property (nonatomic, strong) AutoDictionary *autoDict;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.autoDict = [[AutoDictionary alloc] init]; [self.autoDict setNumber:[NSNumber numberWithInt:30]]; self.autoDict.string = [NSString stringWithFormat:@"i an"]; NSLog(@"number %@,string %@",self.autoDict.number,self.autoDict.string);}
崩溃信息如下:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AutoDictionary setNumber:]: unrecognized selector sent to instance 0x60800001c200
利用运行时动态添加存取方法
#import "AutoDictionary.h"#import <objc/runtime.h>@interface AutoDictionary ()@property (nonatomic, strong) NSMutableDictionary *backStore;@end@implementation AutoDictionary@dynamic string,date,number;-(instancetype)init{ self = [super init]; if (self) { _backStore = [NSMutableDictionary new]; } return self;}+(BOOL)resolveInstanceMethod:(SEL)sel{ NSString *selName = NSStringFromSelector(sel); if ([selName hasPrefix:@"set"]) { class_addMethod(self, sel, (IMP)autoDictSetter, "V@:@"); return YES; }else{ class_addMethod(self, sel, (IMP)autoDictGetter, "@@:"); return YES; }}void autoDictSetter(id self,SEL _cmd,id value){ AutoDictionary *typeSelf = (AutoDictionary *)self; NSMutableDictionary *backStore = typeSelf.backStore; // the selector will be for example ,'setNumber:' // we need to remove ':' & 'set' // SEL类型转为字符串类型 NSString *selectorString = NSStringFromSelector(_cmd); // 可变字符串才可以进行一系列的替换 删除 操作 NSMutableString *key = [selectorString mutableCopy]; // remove the ':' at the end [key deleteCharactersInRange:NSMakeRange(key.length - 1, 1)]; // remove the 'set' prefix [key deleteCharactersInRange:NSMakeRange(0, 3)]; //lower first character NSString *lowercaseFirstChar = [[key substringToIndex:1] lowercaseString]; [key replaceCharactersInRange:NSMakeRange(0, 1) withString:lowercaseFirstChar]; if (value) { [backStore setObject:value forKey:key]; }else{ [backStore removeObjectForKey:key]; }}id autoDictGetter(id self,SEL _cmd){ AutoDictionary *typedSelf = (AutoDictionary *)self; NSMutableDictionary *backStore = typedSelf.backStore; NSString *key = NSStringFromSelector(_cmd); return [backStore objectForKey:key];}@end
0 0
- runtime之@dynamic属性
- runTime之属性关联
- Runtime之分类添加属性
- Objective-C研究之Runtime分析(四)-Dynamic Method Resolution
- Runtime--Dynamic Method Resolution
- iOS runtime方法详解之属性操作
- runtime简单使用之动态添加属性
- runtime之属性和成员变量
- Runtime之动态获取属性列表
- Dynamic 和 Dynamic Language Runtime(DLR)
- Dynamic Language Runtime (DLR) 初深
- 浅谈Dynamic 关键字系列之二:调用属性,方法,字段
- 浅谈Dynamic 关键字系列之二:调用属性,方法,字段
- iOS 开发 深入浅出Runtime运行时之方法动态处理(Dynamic Method Resolution)详解
- OC学习之Runtime之一成员变量和属性
- runtime简单理解之获取成员变量以及属性
- Runtime应用之归档 , KVC , 分类添加属性
- Runtime应用之归档 , KVC , 分类添加属性
- 第十周(3)小学生数学考试系统
- C++ 类关键字public、protected、private区别
- CSS3 基础(012_Box Sizing)
- 3.IPC 机制(一)
- laravel 发邮件 qq配置
- runtime之@dynamic属性
- 自动分配座位
- startActivityForResult的使用
- 基于Socket的UDP和TCP编程介绍
- Java BitSet(存数海量数据时的一个途径)
- ant运行testng
- Eclipse 导入Xutils3.0
- 目标跟踪(一):Visual Object Tracking using Adaptive Correlation Filters(MOSSE Filter)
- angularjs-表单与验证-表单控件的使用