0

How do I add one array into another one? The problem is that I insert this array several times in different "slots", but the array I insert changes, so I get a array with every field looking the same.

Is there an option to get the content of array 1 and put the whole content into one field of array 2?

I think the problem is that the array is somehow looking like this :

NSMutableArray *bigArray = [

0: dynamicArray

1: dynamicArray

2: dynamicArray

3: dynamicArray

]

instead of:

NSMutableArray *bigArray = [

0: contentOfDynamicArray

1: contentOfDynamicArray

2: contentOfDynamicArray

3: contentOfDynamicArray

]

My code:

    twoDArray = [[NSMutableArray alloc] init];
    cat1 = [[NSMutableArray alloc] init];

    for (int k = 0; k < [noDuplicatesCat count]; k++) {
        NSLog(@"cat1: %@", cat1);
        [cat1 removeAllObjects];
        for (int i = 0; i < [parseJSONArray count]; i++) {
            NSMutableArray *temp;
            [temp removeAllObjects];
            temp = [[NSMutableArray alloc] init];
            temp = [parseJSONArray objectAtIndex:i];

            if ([[temp valueForKey:@"category"] isEqual:[noDuplicatesCat objectAtIndex:k]]){
                NSLog(@"equal");
                [cat1 addObject:temp];
            } else {
                NSLog(@"not equal");
            }
        }
        [twoDArray addObject:cat1];
        NSLog(@"2dArray: %@", twoDArray);
    }
5
  • 2
    When you insert an object into an array, you insert a POINTER to the object. If you subsequently modify the object and insert again, you will simply have the SAME object inserted twice. If you want DIFFERENT objects you must CREATE different objects. Commented Feb 12, 2014 at 23:37
  • (And in the above code doing temp = [[NSMutableArray alloc] init]; does not really create a new object, since you write a new value to temp in the very next line, wiping out the new array.) Commented Feb 12, 2014 at 23:39
  • (And you appear to be inserting the same cat1 object into twoDArray again and again.) Commented Feb 12, 2014 at 23:40
  • Thank you for your comment. I found a way to not create another object: [bigarray addObject:[NSMutableArray arrayWithArray:smallaray]]; Commented Feb 13, 2014 at 8:02
  • I hate to disappoint you, but arrayWithArray does create another object. Commented Feb 13, 2014 at 12:09

4 Answers 4

1
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray;

Also, your code is kinda nuts. Don’t use ‘i’ and ‘k’ as variable names, actually describe what kind of index you have. And this:

        NSMutableArray *temp;
        [temp removeAllObjects];
        temp = [[NSMutableArray alloc] init];
        temp = [parseJSONArray objectAtIndex:i];

Makes no sense. The second line removes all objects from an UNinitialized variable, which does nothing. The third creates it, which is fine, but then the fourth overwrites the thing you just created.

Sign up to request clarification or add additional context in comments.

3 Comments

"...from an initialized...": I'm guessing you meant "uninitialized". Good comment.
thanks for comment, i know my code isn't very nice and i try to optimize it. I found a easy solution : [bigarray addObject:[NSMutableArray arrayWithArray:smallaray]];
Well, everyone starts somewhere. My code was pretty strange 30 years ago. Just keep at it, no shame in that.
0

It seems like you are trying to split big record array parseJSONArray by record's category field according to [unique] categories from noDuplicatesCat. The resulting twoDArray has the same order as noDuplicatesCat.

Here is the reduced version:

NSMutableArray *twoDArray = [[NSMutableArray alloc] init];

for (id category in noDuplicatesCat) {
    NSMutableArray *cat1 = [[NSMutableArray alloc] init];
    [twoDArray addObject:cat1];

    for (id item in parseJSONArray)
        if ([[item valueForKey:@"category"] isEqual:category])
            [cat1 addObject:item];
}

Comments

0

Thank you, I just found a possible solution: [bigarray addObject:[NSMutableArray arrayWithArray:smallaray]];

1 Comment

Hmmm. Does the array added to bigarray need to be mutable? Does it need to be a copy of smallarray? (BTW, you are better off using camel-case consistently, e.g. bigArray, smallArray.
0

There's a lot wrong with your code, but I don't want to harsh on you. Kudos to you for jumping in and trying to learn something, and also for putting your code out there. I'm going to try to rewrite what you've done based on my very limited understanding of what your are trying to do.

First, some clues:

 temp = [parseJSONArray objectAtIndex:i]
 // and
 if ([[temp valueForKey:@"category"] ...

lead me to believe that parseJSONArray is an array of NSDictionarys. You probably should use -[NSDictionary objectForKey:], e.g.

NSDictionary *temp = [parseJSONArray objectAtIndex:i]
 // and
 if ([[temp objectForKey:@"category"] ...

I'd also bet that [temp objectForKey:@"category"] will return an NSString.

If these assumptions are correct, this looks kind of like what you are trying to do:

// NSArray *noDuplicatesCat = <array of strings>
NSMutableArray *arrayOfArrayOfJSONDicts = [NSMutableArray array];

for (NSString *cat in noDuplicatesCat) {
    NSMutableArray *gather = [NSMutableArray array];  
    for (NSDictionary *jsonDict in parseJSONArray) {
        if ([jsonDict objectForKey:@"category"] isEqualToString:cat]) {
            [gather addObject:jsonDict];
        }
    }
    [arrayOfArrayOfJSONDicts addObject:gather]
}

I'm not sure what useful purpose that is serving. It might be useful or interesting if you told us, at a higher level, what you were trying to accomplish. Prevent duplicates somehow? You can edit your question to add an addendum.

Regardless, given what I'm seeing right now, you really need to hit the books to avoid programming yourself into a complete mess right now. I'd recommend playing around a lot with NSArrays and NSDictionarys until you really understand what you are doing. Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.