·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> runtime使用技巧三

runtime使用技巧三

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-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反射属性的用法已经了解的差不多了,有问题,欢迎大家留言。多多提宝贵意见。