有没有办法记录一个 Objective-C 实例的所有属性值 [英] Is there a way to log all the property values of an Objective-C instance
本文介绍了有没有办法记录一个 Objective-C 实例的所有属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道是否有一种快速简便的方法可以将属性的所有各种值打印到日志中,以便进行调试.就像我想知道所有 BOOL、浮点数等的值是什么.
I was just wondering if there is a quick and easy way of printing out to the log all of the various values of the properties to my class for debugging purposes. Like I would like to know what the values of all of the BOOLs, floats, etc. are.
推荐答案
This问题似乎可以回答您的问题.
This question seems the have the answer to your question.
我很好奇并做了一个分类:
I got curious and made a catagory:
//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];
到这些属性!
and call [self logProperties];
to those properties!
这篇关于有没有办法记录一个 Objective-C 实例的所有属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文