【问题标题】:How to print (using NSLog) all properties of a class automatically?如何自动打印(使用 NSLog)类的所有属性?
【发布时间】:2015-06-12 13:10:34
【问题描述】:

我认为在objective-c中打印出任何类的所有属性的值是非常困难的,在属性类型复杂的情况下。

但是如果类包含简单类型的属性(比如,NSString、int、double、boolean),有没有办法让NSLog自动代替NSLog手动获取每个属性的值?

更新:

您给我的所有解决方案仍然是手动的。有没有办法像遍历一个类的所有属性,以及 NSLog variable_name 和 variable_value。这正是我所期望的。

【问题讨论】:

  • 你可以覆盖你的类的description方法。
  • 只需添加一个断点。您可以在调试时扩展该对象以获取所有属性
  • @MuhammadAdnan 虽然查找 1-2 个对象属性可能很有用,但如果您需要一次大规模读取多个对象(尤其是嵌套对象),然后在运行时深入到对象中变得乏味,尤其是如果您需要进行微调并运行应用程序大约 20 次,每次都向下钻取。
  • 我的对象是 NSObject

标签: ios objective-c


【解决方案1】:

您可以通过覆盖-(void)description 方法来做到这一点。

示例: 假设我们有简单的Car 类。

@interface Car : NSObject

@property (copy, nonatomic)   NSString *model;
@property (copy, nonatomic)   NSString *make;
@property (strong, nonatomic) NSDate *registrationDate;
@property (assign, nonatomic) NSInteger mileage;
@property (assign, nonatomic) double fuelConsumption;

@end

@implementation
- (NSString*)description {
    return [NSString stringWithFormat:@"<%@:%p %@>",
            [self className],
            self,
            @{ @"model"           : self.model,
               @"make"            : self.make,
               @"registrationDate": self.registrationDate,
               @"mileage"         : @(self.mileage),
               @"fuelConsumption" : @(self.fuelConsumption)
            }];
}
@end

将其放入NSDictionary 将在控制台中创建非常好的输出。

另一方面,您可以在 NSObject 类上创建类别并执行以下操作:

- (NSString*)myDescriptionMethod {
    NSMutableDictionary *dict = [NSMutableDictionary new];
    unsigned int count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);

    for (int i = 0; i < count; i++) {
        const char *property = property_getName(properties[i]);
        NSString *propertyString = [NSString stringWithCString:property encoding:[NSString defaultCStringEncoding]];
        id obj = [self valueForKey:propertyString];
        [dict setValue:obj forKey:propertyString];
    }

    free(properties);
    return [NSString stringWithFormat:@"<%@ %p %@>",
            [self class],
            self,
            dict];
}

那么您将避免在您的类中覆盖-(void)description 方法。

从here获取它

【讨论】:

  • 我得到一个错误:“'objc_property_t'的声明必须先从模块'ObjectiveC.runtime'导入”
  • @chipbk10 使用#import "objc/runtime.h"
【解决方案2】:

使用 NSObject 子类实现您在 Objective-C 中寻找的最优雅的方法,它覆盖 NSObject 方法description。

例如(假设您的 Class 有一个名为 propertyX 的属性):

-(NSString *)description
{
  return [NSString stringWithFormat:@"<myCustomObject: %@, propertyX: %f, %f>",
                 [self objectID], [self propertyX].x, [self propertyX].y];
}

NSObject 的默认 description 实现将简单地返回指向对象的内存地址,如下所示:

NSLog(@"%@", self);

2015-06-15 14:20:30.123 AppName[...] myCustomObject: 0x000000>

但是,通过如上所示重写此基类方法,您将能够自定义此行为,日志将如下所示:

2015-06-15 14:20:30.123 AppName[...] myCustomObject: 0x000000 someProperty, Property: blah, blah>

有一个很好的教程,它进一步讨论了这个here。

【讨论】:

    【解决方案3】:

    示例:-

     + (NSString *)description;
        [NSString description];
    

    为您提供有关 NSString 类的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2011-08-23
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 2021-09-18
      相关资源
      最近更新 更多