1

I want to below effect,but I don't kown how to use NSMutableArray combine NSArray More than two?

1.my code

for (int i=0; i<[DateSortArry2 count]; i++) {
        for (int j=0; j<[DateSortArry2Copy count]; j++) { 
            NSString *sectiondateStr2 = [NSString stringWithFormat:@"%@",[DateSortArry2Copy objectAtIndex:j]];    
            if ([[DateSortArry2 objectAtIndex:i] isEqualToString:sectiondateStr2]) {  
                [Arry addObject:sectiondateStr2];   
            }
        }
        [SumArry addObjectsFromArray:Arry];
        [Arry removeAllObjects];
    }

2.my code Result

SumArry:(
"20130227",
"20130227",
"20130227",
"20130226",
"20130226",
"20130226",
"20130225",
"20130225")

3.I want the results

SumArry:((
    "20130227",
    "20130227",
    "20130227",
    ),
    (
    "20130226",
    "20130226",
    "20130226",
    ),
    (
    "20130225",
    "20130225"
    ))
1
  • 1
    Hey! why the downvotes? Got to help people. Upvoting to counteract the downvotes... Commented Mar 30, 2013 at 3:02

3 Answers 3

2

Your code repeatedly fills and empties the same array by adding its elements, but you need to preserve the structure with additional instances of NSArray. So, use a new NSArray for each section.

for (int i=0; i<[DateSortArry2 count]; i++) {
    NSMutableArray *section = [NSMutableArray array];
    for (int j=0; j<[DateSortArry2Copy count]; j++) { 
        NSString *sectiondateStr2 = [NSString stringWithFormat:@"%@",[DateSortArry2Copy objectAtIndex:j]];    
        if ([[DateSortArry2 objectAtIndex:i] isEqualToString:sectiondateStr2]) {  
            [section addObject:sectiondateStr2];   
        }
    }
    [SumArry addObject:section];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot!sartak!you are Right!
2

You can either store a reference to another array (or any type of object) in your array:

[myMutableArray addObject:otherArray];

Or concatinate the arrays.

[myMutableArray addObjectsFromArray:otherArray];

Both of which are documented in the documentation. By the looks of it the first approach is what you want since you want to have NSArray of NSMutableArray.

Comments

1

try this: please tell me if it works. thanks

NSString *str = @"";
    for (int i=0; i<[DateSortArry2 count]; i++)
     {
           if (str isEqualToString:[DateSortArry2 objectAtIndex:i])
           {
                return;
           }
           else
           {
                NSMutableArray * Arry = [[NSMutableArray alloc] init];
                str = [DateSortArry2 objectAtIndex:i]
                for (int j=0; j<[DateSortArry2Copy count]; j++)
                {
                    if ([[DateSortArry2 objectAtIndex:i] isEqualToString:str])
                    {
                        [Arry addObject:str];
                    }
                }
                [SumArry addObject:Arry];
                [Arry removeAllObjects];
            }
    }

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.