Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何自动打印(使用NSLog)类的所有属性?_Ios_Objective C - Fatal编程技术网

Ios 如何自动打印(使用NSLog)类的所有属性?

Ios 如何自动打印(使用NSLog)类的所有属性?,ios,objective-c,Ios,Objective C,我认为很难打印出objective-c中任何类的所有属性的值,因为属性的类型很复杂 但是,如果类包含简单类型的属性(例如,NSString、int、double、boolean),是否有任何方法可以自动记录而不是手动记录每个属性的值 已更新: 您给我的所有解决方案仍然是手动的。有什么方法可以像遍历类的所有属性一样,记录变量名和变量值。这正是我所期望的。示例:- + (NSString *)description; [NSString description]; 提供有关NSStrin

我认为很难打印出objective-c中任何类的所有属性的值,因为属性的类型很复杂

但是,如果类包含简单类型的属性(例如,NSString、int、double、boolean),是否有任何方法可以自动记录而不是手动记录每个属性的值

已更新

您给我的所有解决方案仍然是手动的。有什么方法可以像遍历类的所有属性一样,记录变量名和变量值。这正是我所期望的。

示例:-

 + (NSString *)description;
    [NSString description];
提供有关NSString类的信息。

示例:-

 + (NSString *)description;
    [NSString description];
提供有关NSString类的信息。

示例:-

 + (NSString *)description;
    [NSString description];
提供有关NSString类的信息。

示例:-

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

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

使用
NSObject
子类,这是实现您在Objective-C中所需的最优雅的方法,它可以覆盖
NSObject
方法
描述

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

-(NSString *)description
{
  return [NSString stringWithFormat:@"<myCustomObject: %@, propertyX: %f, %f>",
                 [self objectID], [self propertyX].x, [self propertyX].y];
}
2015-06-15 14:20:30.123 AppName[…]myCustomObject:0x000000>

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

- (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];
}
2015-06-15 14:20:30.123 AppName[…]myCustomObject:0x000000SomeProperty,Property:blah,blah>


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

通过
NSObject
子类,这是实现所需的最优雅的方法,它可以覆盖
NSObject
方法
描述

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

-(NSString *)description
{
  return [NSString stringWithFormat:@"<myCustomObject: %@, propertyX: %f, %f>",
                 [self objectID], [self propertyX].x, [self propertyX].y];
}
2015-06-15 14:20:30.123 AppName[…]myCustomObject:0x000000>

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

- (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];
}
2015-06-15 14:20:30.123 AppName[…]myCustomObject:0x000000SomeProperty,Property:blah,blah>


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

通过
NSObject
子类,这是实现所需的最优雅的方法,它可以覆盖
NSObject
方法
描述

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

-(NSString *)description
{
  return [NSString stringWithFormat:@"<myCustomObject: %@, propertyX: %f, %f>",
                 [self objectID], [self propertyX].x, [self propertyX].y];
}
2015-06-15 14:20:30.123 AppName[…]myCustomObject:0x000000>

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

- (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];
}
2015-06-15 14:20:30.123 AppName[…]myCustomObject:0x000000SomeProperty,Property:blah,blah>


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

通过
NSObject
子类,这是实现所需的最优雅的方法,它可以覆盖
NSObject
方法
描述

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

-(NSString *)description
{
  return [NSString stringWithFormat:@"<myCustomObject: %@, propertyX: %f, %f>",
                 [self objectID], [self propertyX].x, [self propertyX].y];
}
2015-06-15 14:20:30.123 AppName[…]myCustomObject:0x000000>

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

- (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];
}
2015-06-15 14:20:30.123 AppName[…]myCustomObject:0x000000SomeProperty,Property:blah,blah>


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

您可以通过重写
-(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
然后您将避免在类中重写
-(void)description
方法


获取它,您可以通过重写
-(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
然后您将避免在类中重写
-(void)description
方法


获取它,您可以通过重写
-(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
然后您将避免在类中重写
-(void)description
方法


获取它,您可以通过重写
-(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
然后您将避免在类中重写
-(void)description
方法


获取它,您可以覆盖类的
说明
方法。只需添加一个断点。您可以扩展该对象以获取所有属性debugging@MuhammadAdnan虽然查找1-2个对象属性很有用,但如果需要同时读取多个对象(尤其是嵌套对象),那么在运行时深入到对象中会变得很乏味,另外,如果您需要稍微调整,并运行应用程序大约20次,每次都要向下钻取。我的对象是NSObjects,您可以覆盖类的
说明
方法。只需添加一个断点即可。您可以扩展该对象以获取所有属性debugging@MuhammadAdnan虽然查找1-2个对象属性很有用,但如果需要同时读取多个对象(尤其是嵌套对象),那么在运行时深入到对象中会变得很乏味,另外,如果您需要稍微调整,并运行应用程序大约20次,每次都要向下钻取。我的对象是NSObjects,您可以覆盖类的
说明
方法。只需添加一个断点即可。你可以用它