【问题标题】:Is there a way to log all the property values of an Objective-C instance有没有办法记录一个 Objective-C 实例的所有属性值
【发布时间】:2012-12-17 21:36:17
【问题描述】:

我只是想知道是否有一种快速简便的方法可以将属性的所有各种值打印到日志中以用于调试目的。就像我想知道所有 BOOL、浮点数等的值是什么。

【问题讨论】:

    标签: objective-c properties nslog


    【解决方案1】:

    This question 似乎已经回答了你的问题。

    更新:

    我很好奇,做了一个分类:

    //Using Xcode 4.5.2 - iOS 6 - LLDB - Automatic Reference Counting
    
    //NSObject+logProperties.h    
    @interface NSObject (logProperties)
    - (void) logProperties;
    @end
    
    //NSObject+logProperties.m
    #import "NSObject+logProperties.h"
    #import <objc/runtime.h>
    
    @implementation NSObject (logProperties)
    
    - (void) logProperties {
    
        NSLog(@"----------------------------------------------- Properties for object %@", self);
    
        @autoreleasepool {
            unsigned int numberOfProperties = 0;
            objc_property_t *propertyArray = class_copyPropertyList([self class], &numberOfProperties);
            for (NSUInteger i = 0; i < numberOfProperties; i++) {
                objc_property_t property = propertyArray[i];
                NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];
                NSLog(@"Property %@ Value: %@", name, [self valueForKey:name]);
            }
            free(propertyArray);
        }    
        NSLog(@"-----------------------------------------------");
    }
    
    @end
    

    将其包含在您的课程中:#import "NSObject+logProperties.h"

    并调用[self logProperties]; 到这些属性!

    【讨论】:

    【解决方案2】:

    当前的答案仅显示如何为属性执行此操作。如果您希望打印出每个实例变量,您可以执行以下操作。

    - (void)logAllProperties {
        unsigned int count;
        Ivar *ivars = class_copyIvarList([self class], &count);
        for (unsigned int i = 0; i < count; i++) {
            Ivar ivar = ivars[i];
    
            const char *name = ivar_getName(ivar);
            const char *type = ivar_getTypeEncoding(ivar);
            ptrdiff_t offset = ivar_getOffset(ivar);
    
            if (strncmp(type, "i", 1) == 0) {
                int intValue = *(int*)((uintptr_t)self + offset);
                NSLog(@"%s = %i", name, intValue);
            } else if (strncmp(type, "f", 1) == 0) {
                float floatValue = *(float*)((uintptr_t)self + offset);
                NSLog(@"%s = %f", name, floatValue);
            } else if (strncmp(type, "@", 1) == 0) {
                id value = object_getIvar(self, ivar);
                NSLog(@"%s = %@", name, value);
            }
            // And the rest for other type encodings
        }
        free(ivars);
    }
    

    虽然我不会特别建议在实践中这样做,但如果是出于调试目的,那就没问题了。您可以将其作为NSObject 上的一个类别来实现,并在调试时将其保留以供使用。如果完成了所有类型的编码,那么它可以成为一个非常好的小方法。

    【讨论】:

      【解决方案3】:

      NSObject 上现在有这些方法:

      @interface NSObject (Private)
      -(id)_ivarDescription;
      -(id)_shortMethodDescription;
      -(id)_methodDescription;
      @end
      

      迅速:

      myObject.perform("_ivarDescription")
      

      感谢this article

      【讨论】:

        【解决方案4】:

        是的,一种方法是询问所有属性,然后使用 KVC,例如:

        //properties
        unsigned int cProperties = 0;
        objc_property_t *props = class_copyPropertyList(self.class, &cProperties);
        for(int i = 0; i < cProperties; i++) {
            const char *name = property_getName(props[i]);
            NSLog(@"%@=%@", name, [self valueForKey:name];
        }
        

        另一种方法是遍历一个类的所有方法,获取返回类型,调用并打印它

        【讨论】:

        • 当然这只适用于对象,而不是像浮点数和布尔值这样的原始类型。您将如何解决 NSLog 中格式说明符的问题,该说明符当前记录属性对象和原始类型 (%@) 上的崩溃?
        • 见上面的 matt 回答
        【解决方案5】:

        快速而肮脏的方法是覆盖debugDescription

        -(NSString*)debugDescription {
            NSString *str = [[NSString alloc] initWithFormat:@"My BOOL 1: %d, My Float: %f", self.myBool, self.myFoat];
            return str;
        }
        

        当然,如果您的对象很复杂,这可能会很耗时。

        【讨论】:

        • 仅当您喜欢打字时;)或仅用于一个对象!
        • @Daij-Djan 因此免责声明。
        猜你喜欢
        • 1970-01-01
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多