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!!
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!!
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.
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]);
}
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);
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.