Two Dimension NSMutableArray help?

三世轮回 提交于 2019-12-05 07:42:11

问题


Ok, I have some C# code that looks like this and I was wondering what other developers would recommend if I am trying to put this into Objective-C.

List<List<string>> meta_data

I'm planning on using NSMutableArray but how to exactly get that two-dimensional array figured out is my problem, since there is no such thing as a multidimensional array in Objective-C. I'm new to using NSMutableArray, so I still need some help every now and then.

I know I will just add string objects to the array using NSString once I have the whole "two-dimensional" part figured out.


回答1:


An array can hold any object. I'm not familiar with C# code, but I imagine all your trying to do is nested arrays.

What you need to be using is objectAtIndex: for NSArrays.

NSString *hello = @"Hello World";
NSMutableArray *insideArray = [[NSMutableArray alloc] initWithObjects:hello,nil];
NSMutableArray *outsideArray = [[NSMutableArray alloc] init];
[outsideArray addObject:insideArray];
// Then access it by:
NSString *retrieveString = [[outsideArray objectAtIndex:0] objectAtIndex:0];

I think your looking for something like that. Does that help?




回答2:


Something like this:

//Create the array of strings
NSMutableArray *strings = [[NSMutableArray alloc] init];
[strings addObject:@"someString"];
[strings addObject:@"someOtherString"];

//Create the array to hold the string array
NSMutableArray *container = [[NSMutableArray alloc] init];
[container addObject:strings];



回答3:


You would use a NSMutableArray of NSMutableArrays containing NSStrings



来源:https://stackoverflow.com/questions/1114679/two-dimension-nsmutablearray-help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!