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.
@property offers a way to define the information that a class is intended to encapsulate. If you declare an object/variable using @property, then that object/variable will be accessible to other classes importing its class.
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
Include it in your class: #import "NSObject+logProperties.h"
and call [self logProperties];
to those properties!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With