1

sortedArray contains 100+ objects that need to be organize by the 'hour' in the 'time' key. The outcome is to have an array of arrays to populate the sections of a table and use the hour for the index. 'time' is of the form - 2012-05-15 07:20:00

NSInteger d = 0;
NSInteger prevd;
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
detailArray = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < 24; ++i)
    [detailArray addObject:[NSNull null]];

for (NSArray *item in sortedArray) {
    prevd = d;
    d = [[[[item objectAtIndex:0] objectForKey:@"time"] substringWithRange:NSMakeRange(11, 2)] intValue];
    if (d > prevd && prevd != 0) {
        [detailArray replaceObjectAtIndex:d withObject:tempArray];
        [tempArray removeAllObjects];
    }
    [tempArray addObject:item];
}

The objects are not ending up in the correct detailArray postition or not ending up in it at all. For instance, [detailArray objectAtIndex:6] give items with time 14 (2pm).

What am I missing?

Edit to correct code with 1st answer. It is now:

NSInteger d = 0;
NSInteger prevd;
NSMutableArray *tempArray = nil;
detailArray = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < 24; ++i)
    [detailArray addObject:[NSNull null]];

for (NSArray *item in sortedArray) {
    prevd = d;
    d = [[[[item objectAtIndex:0] objectForKey:@"time"] substringWithRange:NSMakeRange(11, 2)] intValue];
    if (d > prevd) {
        tempArray = NSMutableArray array];
        [detailArray replaceObjectAtIndex:d withObject:tempArray];
    }
    [tempArray addObject:item];
}

1 Answer 1

3

You can't reuse tempArray like that, you're going to end up with detailArray containing multiple entries that all point to the same tempArray instance. (i.e. every slot that had a time is going to end up with the array for the last time slot).

Instead of

[detailArray replaceObjectAtIndex:d withObject:tempArray];
[tempArray removeAllObjects];

you'll want

tempArray = [NSMutableArray array];
[detailArray replaceObjectAtIndex:d withObject:tempArray];

that way each time slot has its own array. And you'll also want to change your original declaration of tempArray to be

NSMutableArray *tempArray = nil;
2
  • These changes worked. A few more answers please: Why the NSMutableArray *tempArray = nil declaration? (I'm assuming that should be line 3 of original) That has what effect? Doesn't tempArray = [NSMutableArray array] create and return a new object each time? Is a new & different tempArray object created when that is run? And second, why the order 'create' and then 'replace'? (the 2 lines in middle group of code) Isn't tempArray empty when its placed in detailArray on the next line?
    – David
    Commented May 16, 2012 at 12:35
  • remember that replaceObjectAtIndex does't copy the array, it just holds a reference to it, so you need to do the create first, otherwise when you put it in detail array, it'll still have the previous slots values in it.
    – superfell
    Commented May 16, 2012 at 15:31

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.