Objects Not being added to array
Objects Not being added to array
- Subject: Objects Not being added to array
- From: "Matthew Callis" <email@hidden>
- Date: Sat, 16 Dec 2006 22:23:20 -0600
Hi,
I have an array of items that I have already collected and now I want to go
through those items and based on the criteria below compare them (they are
already sorted) count them, add up the length, and add each unique item to
the new array. The problem occurs that the first item is skipped entirely
and both the item count and the length is multiplied by 4 (due to some int
conversion error somewhere?). Where am I going wrong?
NSMutableArray *allTapes = [tapesArray arrangedObjects];
if([allTapes count] == 0) return;
NSString *cur_artist = [[allTapes objectAtIndex:0] objectAtIndex:0];
NSString *cur_venue = [[allTapes objectAtIndex:0] objectAtIndex:1];
NSString *cur_location = [[allTapes objectAtIndex:0] objectAtIndex:2];
NSString *cur_genre = [[allTapes objectAtIndex:0] objectAtIndex:3];
NSString *cur_year = [[allTapes objectAtIndex:0] objectAtIndex:4];
// NSString *cur_length = [[allTapes objectAtIndex:0]
objectAtIndex:5];
int *cur_length = 0;
int *cur_count = 0;
NSArrayController * tapeBunch = [[[NSArrayController alloc] init]
retain];
tapeBunch = [[tapeBunch retain] autorelease];
for(i = 0; i < [allTapes count]; i++){
NSComparisonResult artistResult, venueResult;
artistResult = [[[allTapes objectAtIndex:i] objectAtIndex:0]
compare:cur_artist];
venueResult = [[[allTapes objectAtIndex:i] objectAtIndex:1]
compare:cur_venue];
if(artistResult != 0 || venueResult != 0){
[tapeBunch addObject:cur_artist];
[tapeBunch addObject:cur_venue];
[tapeBunch addObject:cur_location];
[tapeBunch addObject:cur_genre];
[tapeBunch addObject:cur_year];
[tapeBunch addObject:[NSString stringWithFormat:@"%i",
cur_length]];
[tapeBunch addObject:[NSString stringWithFormat:@"%i",
cur_count]];
cur_artist = [[allTapes objectAtIndex:i]
objectAtIndex:0];
cur_venue = [[allTapes objectAtIndex:i] objectAtIndex:1];
cur_location = [[allTapes objectAtIndex:i] objectAtIndex:2];
cur_genre = [[allTapes objectAtIndex:i] objectAtIndex:3];
cur_year = [[allTapes objectAtIndex:i] objectAtIndex:4];
cur_length = [[[allTapes objectAtIndex:i]
objectAtIndex:5] intValue];
cur_count = 1;
}
else{
cur_count++;
cur_length += [[[allTapes objectAtIndex:i]
objectAtIndex:5] intValue];
}
}
NSMutableArray *orderedTapes = [tapeBunch arrangedObjects];
NSLog(@"orderedTapes:\n %@", orderedTapes);
They log output is this:
orderedTapes:
("The Arcade Fire", "Neon Bible", None, Indie, 0, 1016, 4)
but should look more like this:
orderedTapes:
("The Arcade Fire", "Funerals", None, Indie, 2003, 2400, 10),
("The Arcade Fire", "Neon Bible", None, Indie, 2007, 220, 1)
--
- Matthew
http://eludevisibility.org/
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden