I'm trying to create 2D array and initialize it with NSStrings. When I try to copy content of a cell from the array to a label.text, the application crashes.
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
[array addObject:[NSMutableArray arrayWithObjects:
[NSArray arrayWithObjects: @"0-0", @"0-1", @"0-2", nil],
[NSArray arrayWithObjects: @"1-0", @"1-1", @"1-2", nil],
[NSArray arrayWithObjects: @"2-0", @"2-1", @"2开发者_运维百科-2", nil],
nil]];
label.text = [[array objectAtIndex:0] objectAtIndex:0];
Any idea why and what am I doing wrong?
You're creating a 3D array (array of array of array), not a 2D array (array of array). Use this:
[array addObject:[NSArray arrayWithObjects: @"0-0", @"0-1", @"0-2", nil]];
[array addObject:[NSArray arrayWithObjects: @"1-0", @"1-1", @"1-2", nil]];
[array addObject:[NSArray arrayWithObjects: @"2-0", @"2-1", @"2-2", nil]];
or this:
NSMutableArray* array = [NSMutableArray arrayWithObjects:
[NSArray arrayWithObjects: @"0-0", @"0-1", @"0-2", nil],
[NSArray arrayWithObjects: @"1-0", @"1-1", @"1-2", nil],
[NSArray arrayWithObjects: @"2-0", @"2-1", @"2-2", nil],
nil]];
精彩评论