0

I have an NSMutable array which contains some objects and I want to store these objects into a different Mutable array in the form of different arrays.

For ex: I have an array named sessionArrayType4 which has these objects.

"New Attendee Reception",
"Attendee Reception",
"Banquett",
"Tour and BBQ" 

And I want to store in another Mutable array named globalSessionArray and I'm expecting result something like below.

(("New Attendee Reception"), ("Attendee Reception"), (Banquett), (Tour and BBQ))

Here is my code:

if(self.pickerSelectedRow == [self.typePickerArray objectAtIndex:3])
{
    for (int i =0; i< [self.sessionArrayType4 count]; i++)
    {
        if(self.displayTime == [[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"start_time"])
        {
            [self.sessionRowArray addObject:[[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"session_name"]];
            self.rowCount = self.rowCount + 1;
        }
        else
        {
            [self.sessionRowArray removeAllObjects];
            self.displayTime = [[self.sessionArrayType4 objectAtIndex:i] valueForKey:@"start_time"];
            [self.sessionRowArray addObject:[[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"session_name"]];

            [self.globalSessionArray addObject:self.sessionRowArray];

        }
    }
}

But I am getting output something like this:

((Tour and BBQ), (Tour and BBQ), (Tour and BBQ), (Tour and BBQ))

10
  • replace if(self.displayTime == [[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"start_time"]) with if([self.displayTime isEqualToString:[[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"start_time"]]) Commented May 7, 2013 at 12:12
  • Can you please describe your objects better; what you have described is an array of strings, but your code shows that you are actually storing "proper" objects. Please post the definition of that object. Commented May 7, 2013 at 12:12
  • @Girish : i understand that but how does that going to make any effect on my output coz any way it's going inside the loop. Commented May 7, 2013 at 12:15
  • @Zac24 I just point out one mistake from your code. Commented May 7, 2013 at 12:17
  • oh okay.. but i need some other solution. thx anyway. Commented May 7, 2013 at 12:19

3 Answers 3

1

You're going to have to alloc/init your values into an object for each iteration of the for loop or else it will continually stick in memory and overwrite itself.

if(self.pickerSelectedRow == [self.typePickerArray objectAtIndex:3])
{
    NSArray *
    for (int i =0; i< [self.sessionArrayType4 count]; i++)
    {
        NSObject *myNewObject = [[NSObject alloc] init];
        if(self.displayTime == [[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"start_time"])
        {
            myNewObject = [[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"session_name"];
            [self.sessionRowArray addObject: myNewObject];
            self.rowCount = self.rowCount + 1;
        }
        else
        {
            [self.sessionRowArray removeAllObjects];
            //NSLog(@"%@",self.sessionRowArray);
            self.displayTime = [[self.sessionArrayType4 objectAtIndex:i] valueForKey:@"start_time"];
            myNewObject = [[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"session_name"]];
            [self.sessionRowArray addObject:myNewObject];
            // NSLog(@"%@",self.sessionRowArray);
            [self.globalSessionArray addObject:self.sessionRowArray];
            // NSLog(@"%@",self.globalSessionArray);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

[self.globalSessionArray addObject:self.sessionRowArray]; this line add Array in to array. my frnd
1

You need to allocate an temp array in for loop to get a final array of arrays.

The code is very confusing but you can modify like below to get correct result:

    if(self.pickerSelectedRow == [self.typePickerArray objectAtIndex:3])
    {
         for (int i =0; i< [self.sessionArrayType4 count]; i++)
         {
             if(self.displayTime == [[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"start_time"])
         {
        [self.sessionRowArray addObject:[[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"session_name"]];
        self.rowCount = self.rowCount + 1;
    }
     else
     {
         [self.sessionRowArray removeAllObjects];
         NSMutableArray* tempArray = [[NSMutableArray alloc] init];
        //NSLog(@"%@",self.sessionRowArray);
        self.displayTime = [[self.sessionArrayType4 objectAtIndex:i] valueForKey:@"start_time"];
        [tempArray addObject:[[self.sessionArrayType4 objectAtIndex:i]valueForKey:@"session_name"]];
        // NSLog(@"%@",self.sessionRowArray);
        [self.globalSessionArray addObject:tempArray];
        // NSLog(@"%@",self.globalSessionArray);
    }

1 Comment

if you are allocating a temporary array and not adding it's object to globalSessionArray then what's the point of doing this.?
0

1)

if(self.pickerSelectedRow == [self.typePickerArray objectAtIndex:3])

This expression and similar ones look like incorrect, because the left value look likes int or NSInteger and right value is NSObject. If they are both NSObjects then use isEqual instead.

2)

sessionArrayType4

it is correct, but no serious programmer uses such names for variables.

3)It is hard to understand what do you want because of incomplete code and a lot of NSLog calls. You have also added JSON code which is particularly incompatible with a previous part of code.

So edit your code first

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.