Two Dimension NSMutableArray help?
-
12-09-2019 - |
Question
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.
Solution
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?
OTHER TIPS
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];
You would use a NSMutableArray of NSMutableArrays containing NSStrings