I am a web developer trying to make it in an Xcode world, and I need to see the contents of an array I have in my console, what are my options?
Asked
Active
Viewed 1,837 times
1
-
See http://stackoverflow.com/questions/2299841/objective-c-introspection-reflection – neoneye Oct 05 '12 at 09:41
4 Answers
6
There's no need to iterate through an array just to print it.
All the collection types have a -description method that returns a NSString of their contents. Just use the object format specifier %@
NSLog(@"%@", array);

willc2
- 38,991
- 25
- 88
- 99
1
As an additional note you can dynamically print NSArray
s and other objects in the debugger using po object
. This uses the same description
method that NSLog does. So it's not always necessary to litter your code with NSLog
s, especially if you're already in the debugger.

Jon Shier
- 12,200
- 3
- 35
- 37
0
You could try something like this:
NSArray *array = [NSArray arrayWithObjects: @"a", @"b", @"Hello World", @"d", nil];
for (id obj in array) {
NSLog(@"%@", obj);
}
... which would log each item in the array to the console in their own separate NSLog
messages.

esqew
- 42,425
- 27
- 92
- 132
0
Or if you want to see your NSDictionary
's content (which is comparable to a PHP associated array()
), you could use:
for (id key in [dictionary allKeys]) {
NSLog(@"Key: %@, Value: %@", key, [dictionary objectForKey:key]);
}

Douwe Maan
- 6,888
- 2
- 34
- 35