Eduardito
Eduardito

Reputation:

Using a NSMutableArray to store data

Can you store an Array ( that contains another two subarrays one being a string and another another array) into a single MutableArray object?

Upvotes: 1

Views: 2190

Answers (3)

James Eichele
James Eichele

Reputation: 119116

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

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

edit: Assuming that myArray is defined as follows:


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 I posted above will give you one big NSMutableArray which contains:

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

Upvotes: 0

Marc Charbonneau
Marc Charbonneau

Reputation: 40507

Yes, you can store any type of object into one of the NSArray classes. The only difficulty is in conceptually dealing with how to access this data structure; complex nesting of arrays within arrays can be hard to manage and lead to bugs. Make sure your needs aren't better solved by another data structure, or a custom class.

Upvotes: 3

August
August

Reputation: 12187

Please read the documentation on NSArray. It can contain any number of arbitrary objects.

That said, without knowing more about what you're doing, I'd suggest you look at NSDictionary and its mutable subclass.

Upvotes: 3

Related Questions