开发者

Accessing 2D array and passing string to label.text

开发者 https://www.devze.com 2022-12-29 02:03 出处:网络
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.

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]];
0

精彩评论

暂无评论...
验证码 换一张
取 消