我需要创建一个数组,其中每个对象都是NSArray:
NSMutableArray *AberdeenStores=[NSMutableArray alloc];
AberdeenStores=[AberdeenStores initWithObjects:[[NSArray alloc]initWithObjects:@"Aberdeen Name Store 1",@"Aberdeen Adress Store 1",@"Aberdeen Telephone store 1","", nil],[NSArray arrayWithObjects:@"Aberdeen Name Store 2",@"Aberdeen Adress Store 2",@"Aberdeen Telephone store 2", nil],[[NSArray alloc ]initWithObjects:@"Aberdeen Name Store 3",@"Aberdeen Adress Store 3",@"Aberdeen Telephone store 3", nil], nil];
这似乎导致崩溃。有什么问题吗?提前完成。
答案 0 :(得分:4)
你那里只有一个错字。在第一个子数组
中用“”替换“”NSMutableArray *AberdeenStores=[NSMutableArray alloc];
AberdeenStores=[AberdeenStores initWithObjects:[[NSArray alloc]initWithObjects:@"Aberdeen Name Store 1",@"Aberdeen Adress Store 1",@"Aberdeen Telephone store 1",@"", nil],[NSArray arrayWithObjects:@"Aberdeen Name Store 2",@"Aberdeen Adress Store 2",@"Aberdeen Telephone store 2", nil],[[NSArray alloc ]initWithObjects:@"Aberdeen Name Store 3",@"Aberdeen Adress Store 3",@"Aberdeen Telephone store 3", nil], nil];
答案 1 :(得分:1)
这是一个更具伸缩性的解决方案:
// Top Level Array
NSMutableArray *array = [[NSMutableArray alloc] init];
int size = 10;
// Build sub level arrays
for(int i = 0; i < size; i++)
{
NSMutableArray *subArray = [[NSMutableArray alloc] init];
[array addObject:subArray];
}
// You can now use it like this:
[[array objectAtIndex:0] addObject:someObject];
NSObject *someObject = [[array objectAtIndex:0] objectAtIndex:0];
希望有所帮助!
答案 2 :(得分:0)
你在这里使用的是变量和对象以及所有错误。这是做你正在尝试做的最好的方法,并且布局得很好,并且更好的可维护性。
NSMutableArray *AberdeenStores = [[NSMutableArray alloc] init];
[AberdeenStores addObject:[NSArray arrayWithObjects:@"Aberdeen Name Store 1",@"Aberdeen Adress Store 1",@"Aberdeen Telephone store 1",@"", nil]];
[AberdeenStores addObject:[NSArray arrayWithObjects:@"Aberdeen Name Store 2",@"Aberdeen Adress Store 2",@"Aberdeen Telephone store 2",@"", nil]];
[AberdeenStores addObject:... keep adding objects as you please];