美文网首页
Description格式化

Description格式化

作者: 靠北的北 | 来源:发表于2020-08-17 11:32 被阅读0次

    runtime class 属性

    // ...
    #import <objc/runtime.h>
    // ...
    
    - (NSString *)description{
       unsigned int count ,i;
       objc_property_t *propertyArray = class_copyPropertyList([self class], &count);
       NSMutableDictionary *tmpDic = [NSMutableDictionary dictionary];
       for (i = 0; i < count; i++) {
           objc_property_t property = propertyArray[i];
           NSString *proKey = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
           id proValue = [self valueForKey:proKey];
           
           if (proValue) {
               [tmpDic setObject:proValue forKey:proKey];
           } else {
               [tmpDic setObject:@"" forKey:proKey];
           }
       }
       free(propertyArray);
       return  [NSString stringWithFormat:@"%@: %p, \n%@", [self class], self, tmpDic];
    }
    
    

    相关文章

      网友评论

          本文标题:Description格式化

          本文链接:https://www.haomeiwen.com/subject/bdafjktx.html