Sorry for the simple question, but I am self taught and know that there are gaps in my education.
To print an array in objective C, I believe is:
NSLog(@"My array: %@", myArray);
How can I print an array of arrays?
Thanks
You don't need to do anything different to log an array of arrays; the code exactly as you've written it will already show the contents of the sub-arrays.
That is, the following program:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *array = [NSMutableArray array];
for (int i=0; i<5; ++i) {
NSMutableArray *sub = [NSMutableArray array];
for (int j=0; j<=i; ++j) {
[sub addObject:[NSString stringWithFormat:@"%d", j]];
}
[array addObject:sub];
}
NSLog(@"Array: %@", array);
[pool drain];
return 0;
}
Produces the following output:
Array: (
(
0
),
(
0,
1
),
(
0,
1,
2
),
(
0,
1,
2,
3
),
(
0,
1,
2,
3,
4
)
)
Clearly, it's already logging the sub-arrays just fine. If you want to control the formatting differently, you'd have to manually iterate them, but by default, the -description of an NSArray is little more than the -description of every object in that array, which includes all sub-arrays.
NSObject's -description method to make it something more appropriate for that class.So I was embarrassed by the recursiveDescription thing, so I wrote my own as a category on NSArray. Note that this code will print out a description for an array of arrays to any depth. The description itself could probably use a bit more formatting than commas and newlines. Here you go:
@interface NSArray (RecursiveDescription)
- (NSString *)recursiveDescription;
@end
@implementation NSArray (RecursiveDescription)
- (NSString *)recursiveDescription {
NSMutableString *description = [[NSMutableString alloc] initWithString:@"Array (\n"];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (NSObject *child in self) {
if ([child respondsToSelector:@selector(recursiveDescription)]) {
[description appendFormat:@"%@,\n", [child recursiveDescription]];
}
else {
[description appendFormat:@"%@,\n", [child description]];
}
}
[pool drain];
[description appendString:@"\n)"];
return [description autorelease];
}
@end
Try logging the return value from NSArray's -description method.
NSLog(@"My array: %@", [myArray description]);
%@ format specifier uses the result of the description method (or CFCopyDescription() for CoreFoundation objects).As much as I like how easy it is to log out an object in Objective-C, I didn't like seeing a 2D array as a very long list. I created a category on NSArray that prints out 2D arrays. It's not perfect and can be improved, but it has worked for me.
Header:
@interface NSArray (Logging)
- (void)log2DArray;
@end
Implementation:
#import "NSArray+Logging.h"
@implementation NSArray (Logging)
- (void)log2DArray {
NSMutableString *formattedString = [[NSMutableString alloc] init];
NSInteger longestSubarrayLength = 0;
for (NSArray *subarray in self) {
if (subarray.count > longestSubarrayLength) {
longestSubarrayLength = subarray.count;
}
}
for (int i = 0; i < longestSubarrayLength; i++) {
[formattedString appendFormat:@"\n"];
for (int j = 0; j < self.count; j++) {
NSArray *tempArray = [self objectAtIndex:j];
if (tempArray.count <= longestSubarrayLength) {
[formattedString appendFormat:@"%@\t", [tempArray objectAtIndex:i]];
} else {
[formattedString appendFormat:@"\t"];
}
}
}
NSLog(@"%@", formattedString);
}
@end
Usage:
[myArray log2DArray];
Or use recursiveDescription :)
NSLog(@"my arrays: %@", [myArray recursiveDescription]);
NSArray because it doesn't work.
NSArrayofNSArrays? Or do you mean something likeconst char stuff[3][2]? Because the two are very different things.