Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

objective-c multi-dimensional array

I want to use a variable-sized multi-dimensional array in my app to save data. The data structure I want to use is as below, the first element in each row is followed by corresponding multiple values.

 array = {   {a, a_val1, a_val2, a_val3}.  
             {b, b_val1},  
             {c, c_val1, c_val2, c_val3, c_val4, c_val5}  
         }

Any idea how I can implement in objective-c?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

use NSMutableArray like so

NSMutableArray *curRow; /* use to access the row while loading with objects */
NSMutableArray *array = [[NSMutableArray alloc] init]; /* your main multidim array */
curRow = [NSMutableArray array];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[array addObject:curRow]; /* first row is added */

/* rinse and repeat */
curRow = [NSMutableArray array];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[array addObject:curRow];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...