首页 > 学院 > 开发设计 > 正文

runtime使用技巧三

2019-11-14 20:29:23
字体:
来源:转载
供稿:网友

runtime反射属性列表:

把反射属性的用法说完。。。

IOS网络请求大家应该很不陌生了把,但有时候我们需要向服务器传递的数据比较多,这个时候,也是我们runtime发挥的时候了。

直接上代码,童鞋们估计快该骂人了。一个反射属性,讲这么久。。。 

-(void)createPostDateWithDic{    NSArray * arr = [self.requestDictionary allKeys];    for (int i = 0; i < arr.count; i ++) {        if ([self.requestDictionary objectForKey:[arr objectAtIndex:i]] != nil) {            [self.mainRequest setPostValue:[self.requestDictionary objectForKey:[arr objectAtIndex:i]] forKey:[arr objectAtIndex:i]];        }    };}-(void)createPostDateWithModel{    unsigned int outCount, i;    objc_PRoperty_t *properties = class_copyPropertyList([self.requestModel class], &outCount);    for (i=0; i<outCount; i++) {        objc_property_t property = properties[i];        NSString * key = [[NSString alloc]initWithCString:property_getName(property)  encoding:NSUTF8StringEncoding];        id value = [self.requestModel valueForKey:key];        if (value != nil) {            [self.mainRequest setPostValue:value forKey:key];        }    }}

上面是两个方法都是自己封装的POST网络请求中使用的,基于ASI,这两个方法的使用,会使得你的网络请求的代码实现,十分的简介,高效。

好了童鞋们相信到了这里,你对runtime反射属性的用法已经了解的差不多了,有问题,欢迎大家留言。多多提宝贵意见。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表