Adam Lee
Adam Lee

Reputation: 25778

How to quickly print out all the properties in a NSObject and its derivatives?

Say, I have an object of UIColor, can I quickly print out some debug information of its property information?

I need to print properties like red, blue, green components.

Upvotes: 0

Views: 1474

Answers (2)

Hot Licks
Hot Licks

Reputation: 47759

For many objects you can simply use NSLog:

NSLog(@"The object = %@", someObject);

"Under the covers" this invokes the description method of "someObject", which for many objects (notably NSArrays and NSDictionaries) is quite descriptive, but for others, not so.

For your own classes you can override description to produce an appropriate string:

- (NSString*)description {
    return [NSString stringWithFormat:@"MyClass{X = %d, Y = %d}", self.X, self.Y);
}

Upvotes: 1

Mick MacCallum
Mick MacCallum

Reputation: 130222

If you just log a UIColor object like so:

UIColor *color = [UIColor redColor];
NSLog(@"%@",color);

It will output UIDeviceRGBColorSpace 1 0 0 1 which represents RGBA value of the color object. These numbers range from 0 to 1.

Upvotes: 2

Related Questions