In ruby, I can .inspect from an object to know the details. How can I do the similar thing in objective c? Thank you.
-
Which is the real question? this one or that one http://stackoverflow.com/questions/3597186/how-can-i-inspect-an-objective-c-object, either way spamming with duplicates is frowned upon... – t0mm13b Aug 30 '10 at 00:42
-
@tommieb75: given the questions' identical posting times and contents, I'd say it was a simple double-posting error. – Michael Petrotta Aug 30 '10 at 00:50
-
Sorry, I think my network is unstable, so it posts two questions. – Tattat Aug 30 '10 at 00:55
-
2The other question has been closed, merged, and deleted, so I'm going to momentarily close this just to clear the close votes. – Bill the Lizard Aug 30 '10 at 01:34
5 Answers
-[NSObject description]
provides a basic description of an object (similar to toString
in Java--I don't really know about .inspect
in Ruby). description
is automatically called in when you print an object in NSLog
(e.g. NSLog(@"@%", myObject)
).
For other introspection methods, I'd suggest looking at the NSObject reference. There are also a lot of things you can do directly with the Objective-C runtime.

- 7,332
- 4
- 37
- 42
If you just want something to print you can use description
as said before.
I'm not a Ruby guy myself, but if I understand this correctly .inspect
in Ruby prints all the instance variables of an object. This is not something built into Cocoa. If you need this you can use the runtime system to query this information.
Here is a quick category I put together which does that:
#import <objc/objc-class.h>
@interface NSObject (InspectAsInRuby)
- (NSString *) inspect;
@end
@implementation NSObject (InspectAsInRuby)
- (NSString *) inspect;
{
NSMutableString *result = [NSMutableString stringWithFormat: @"<%@:%p", NSStringFromClass( [self class] ), self ];
unsigned ivarCount = 0;
Ivar *ivarList = class_copyIvarList( [self class], &ivarCount );
for (unsigned i = 0; i < ivarCount; i++) {
NSString *varName = [NSString stringWithUTF8String: ivar_getName( ivarList[i] )];
[result appendFormat: @" %@=%@", varName, [self valueForKey: varName]];
}
[result appendString: @">"];
free( ivarList );
return result;
}
@end

- 22,475
- 4
- 52
- 71
Just print it out with NSLog
NSLog(@"%@", myObject);
It will automatically call the object's description
method. If this is a class you created, you will want to define that (return an NSString
with the info).
Take a look at this question.

- 1
- 1

- 36,800
- 16
- 93
- 123
In your NSObject's h file write this :
- (NSDictionary *)dictionaryRepresentation;
In your NSObject's m file write this :
(NSDictionary *)dictionaryRepresentation { unsigned int count = 0; // Get a list of all properties in the class. objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:count];
for (int i = 0; i < count; i++) { NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])]; NSString *value = [self valueForKey:key];
// Only add to the NSDictionary if it's not nil. if (value) [dictionary setObject:value forKey:key];
}
free(properties);
return dictionary; }
(NSString *)description { return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]]; }

- 4,194
- 28
- 32