抱歉这个简单的问题,但我自学成才,知道我的教育存在差距.
要在目标C中打印数组,我相信是:
NSLog(@"My array: %@", myArray);
Run Code Online (Sandbox Code Playgroud)
如何打印阵列数组?
谢谢
aqu*_*qua 10
你要这个:
for(NSArray *subArray in myArray) {
NSLog(@"Array in myArray: %@",subArray);
}
Run Code Online (Sandbox Code Playgroud)
这适用于具有嵌套一层深度的数组的数组.
您不需要做任何不同的事情来记录数组数组; 完全按照你编写的代码,它已经显示了子数组的内容.
也就是说,以下程序:
#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;
}
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
Array: (
(
0
),
(
0,
1
),
(
0,
1,
2
),
(
0,
1,
2,
3
),
(
0,
1,
2,
3,
4
)
)
Run Code Online (Sandbox Code Playgroud)
显然,它已经很好地记录了子阵列.如果要以不同方式控制格式,则必须手动迭代它们,但默认情况下,-descriptionNSArray的数量只是-description该数组中包含所有子数组的每个对象的数量.