Using NSMutableArray to Store Data

Can you store an array (containing two more subarrays, one of which is a string and the other another array) into one MutableArray object?

+1


source to share


3 answers


Read the documentation for NSArray. It can contain any number of arbitrary objects.



However, without knowing more about what you are doing, I suggest you take a look at NSDictionary and its mutable subclass.

+3


source


Yes, you can store any type of object in one of the NSArray classes. The only difficulty lies in conceptually considering how to access this data structure; complex nesting of arrays into arrays can be difficult and error prone. Make sure your needs are not better addressed by another data structure or custom class.



+3


source


Yes. You can use the following (huge) line of code:

NSMutableArray * completeArray =
    [NSMutableArray arrayWithArray:
        [[myArray objectAtIndex:0] arrayByAddingObjectsFromArray:
            [myArray objectAtIndex:1]]];
      

edit: Assuming that's myArray

defined like this:


NSArray * stringArray = [NSArray arrayWithObjects:
                         @"one", @"two", @"three", nil];
NSArray * otherArray = [NSArray arrayWithObjects:
                        someObj, otherObj, thirdObj, nil];
NSArray * myArray = [NSArray arrayWithObjects:
                     stringArray, otherArray, nil];
      

The line of code posted above will give you one big one NSMutableArray

that contains:

@"one, @"two", @"three", someObj, otherObj, thirdObj

0


source







All Articles