class_copyPropertyList与class_copyIvarList区别,获取属性,使用runtime方法的注意事项

class_copyPropertyList返回的仅仅是对象类的属性(@property申明的属性),而class_copyIvarList返回类的所有属性和变量(包括在@interface大括号中声明的变量),下面做个简单的测试。首先,定义一个WFrequencyManager类            

                


         然后在测试类中写一个测试函数testProperties调用上述两个函数得到其返回结果再分别依次遍历输出其返回值
               
       执行上述测试函数后在控制台输出结果为:
             
        从上述执行结果可以很好的说明前者只获取由@property声明的属性,而后者不但获取了@property属性,而且还获取了@interface大括号中声明的变量

 

属性(Property)类型定义了对描述属性的结构体objc_property的不透明的句柄。
typedef struct objc_property *Property;
您可以使用函数class_copyPropertyList和protocol_copyPropertyList来获得类(包括范畴类)或者协议类中的属性列表:
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)

objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned int *outCount)

在使用这些方法的过程中,千万不要忘了一件事,那就是内存释放问题

容易造成这方面错误的有两种原因

一、当我们习惯了ARC的自动释放机制,所以会不经意的遗漏这方面的问题

记得在最后使用free来释放

 

二、大家在写代码的过程,基本都是网上搜索,参考已有的代码

而网上的代码有很大的问题,在代码的完整性方面有所欠缺,比如以下的代码:

 

上面的代码就缺少释放的功能,大家在借鉴时就会调入坑中

 

 

 

如何获取NSObject属性名和属性值的字典

 

最近在利用SBJSON开发的过程中,发现SBJSON无法支持自定义的对象,为此考虑到了两种实现方案。一种在SBJSON框架一层实现一个自定义对象的Category以支持proxyForJson的方法。另一种方案就是应用层将自定义对象转换成属性名和属性值的字典后再交由SBJSON处理。鉴于本次SBJSON由一个底层库维护,折中方案就是在应用层进行自定义对象的处理。经过一番调查和搜索后,发现如下的实现方法:


  1. #import <Foundation/Foundation.h>   
  2. #import <objc/runtime.h>   
  3.  
  4. @interface NSObject (PropertyListing)   
  5.  
  6. // aps suffix to avoid namespace collsion   
  7. //   ...for Andrew Paul Sardone   
  8. - (NSDictionary *)properties_aps;   
  9.  
  10. @end   
  11.  
  12. @implementation NSObject (PropertyListing)   
  13.  
  14. - (NSDictionary *)properties_aps {   
  15.     NSMutableDictionary *props = [NSMutableDictionary dictionary];   
  16.     unsigned int outCount, i;   
  17.     objc_property_t *properties = class_copyPropertyList([self class], &outCount);   
  18.     for (i = 0; i &lt; outCount; i++) {   
  19.         objc_property_t property = properties[i];   
  20.         NSString *propertyName = [[[NSString alloc] initWithCString:property_getName(property)] autorelease];   
  21.         id propertyValue = [self valueForKey:(NSString *)propertyName];   
  22.         if (propertyValue) [props setObject:propertyValue forKey:propertyName];   
  23.     }   
  24.     free(properties);   
  25.     return props;   
  26. }   
  27.  
  28. @end 

    利用一些JSON框架进行自定义对象传输时处理如下:


  1. // The Person class has `firstName` and `lastName`   
  2. // properties.   
  3. // andrew is a Person instance with NSString values   
  4. // of "Andrew" and "Sardone" for `firstName` and   
  5. // `lastName` respectively.   
  6.  
  7. NSString *jsonString = [[andrew properties_aps]   
  8.                          JSONRepresentation];   
  9.  
  10. // now `jsonString` looks like:   
  11. //     { "firstName": "Andrew", "lastName": "Sardone" }  

 

 

- (void)copyProperty:(ViewModel *)dst cls:(Class)cls

{

  

    unsigned int outCount, i;

    objc_property_t *properties = class_copyPropertyList(cls, &outCount);

    

    for(i = 0; i < outCount; i++) {

        objc_property_t property = properties[i];

       

        const char *propName = property_getName(property);

        NSString *propertyName = [NSString stringWithUTF8String:propName];

        

        id val = [self valueForKey:propertyName];

     //这里的respondsToSelector:NSSelectorFromString([self getSetterName:propertyName])中给的是setProperty方法,而不是一个property属性字符串。

        if ((val)  && ([dst respondsToSelector:NSSelectorFromString([self getSetterName:propertyName])]))

        {

            [dst setValue:val forKey:propertyName];

        }

    }

    free(properties);

    

    if ([cls superclass] != [NSObject class])

    {

        [self copyProperty:dst cls:[cls superclass]];

    }

}

 

- (ViewModel *)clone

{

   

    ViewModel *newModel = [[self.class alloc] init];

   

    [self copyProperty:newModel cls:self.class];

    

    return newModel;

}

 

/** 将属性名转换成属性的Setter名,形成set方法 */

-(NSString *)getSetterName:(NSString *)propName{

    NSString *capitalisedSentence = [propName stringByReplacingCharactersInRange:NSMakeRange(0,1)

                                                                      withString:[[propName substringToIndex:1capitalizedString]];

    NSString *setterName = [NSString stringWithFormat:@"set%@:", capitalisedSentence];

    return setterName;

}

posted @ 2018-05-19 10:13  sundaysios  阅读(446)  评论(0)    收藏  举报