views:

44

answers:

4

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??

Thanks!!

A: 

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
What is wrong with `NSLog(@"array = %@", array);` ?
JeremyP
A: 

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
+4  A: 

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
+1  A: 

As an additional note you can dynamically print NSArrays 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 NSLogs, especially if you're already in the debugger.

jshier