Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

NSMutableArray count

CarlosTCarlosT Posts: 118New Users
edited March 2009 in iPhone SDK Development
I have an array and I am removing objects from it using
NSLog(@"%i", theArray.count); // returns a value n
...
[theArray removeObjectAtIndex:i];
NSLog(@"%i", theArray.count); // returns the same value even when i have removed an object!

when I then do a count on the array the number never goes down! surley if i remove an object the count should move down by 1 or does removing an object leave a nil value. If it does, how can I remove that nil also! If i can;t do this my array will grow and grow each time I add an object, but never shrink when I remove one! help!!
Post edited by CarlosT on
My App site <a href="http://thetappyappco.com/" target="_blank">http://thetappyappco.com/</a>;

Replies

  • smashersmasher Posts: 3,859Registered Users @ @ @ @ @
    edited March 2009
    CarlosT wrote: »
    I have an array and I am removing objects from it using
    NSLog(@"%i", theArray.count); // returns a value n
    ...
    [theArray removeObjectAtIndex:i];
    NSLog(@"%i", theArray.count); // returns the same value even when i have removed an object!
    

    when I then do a count on the array the number never goes down! surley if i remove an object the count should move down by 1 or does removing an object leave a nil value. If it does, how can I remove that nil also! If i can;t do this my array will grow and grow each time I add an object, but never shrink when I remove one! help!!

    Something is fishy. It absolutely should shrink the array and move everything down. Are you sure it's an NSMutableArray, and not an NSArray? Are you sure you're not trying to remove index 5 from an array with 5 items? They're numbered 0-4, so there is no index 5.
    	NSMutableArray *theArray = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three",nil];
    	NSLog(@"before remove: %i", theArray.count); // returns three
    	
    	int i=1; 
    	[theArray removeObjectAtIndex:i]; // remove index 1; the string "two"
    	
    	NSLog(@"after remove: %i", theArray.count); // returns two. 
    	NSLog(@"%@", theArray); // only prints two items 
    
    TinyCo is Hiring Mobile Game Programmers (C++)
    http://jobvite.com/m?3Ho5wgwr
  • CarlosTCarlosT Posts: 118New Users
    edited March 2009
    smasher wrote: »
    Something is fishy. It absolutely should shrink the array and move everything down. Are you sure it's an NSMutableArray, and not an NSArray? Are you sure you're not trying to remove index 5 from an array with 5 items? They're numbered 0-4, so there is no index 5.
    	NSMutableArray *theArray = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three",nil];
    	NSLog(@"before remove: %i", theArray.count); // returns three
    	
    	int i=1; 
    	[theArray removeObjectAtIndex:i]; // remove index 1; the string "two"
    	
    	NSLog(@"after remove: %i", theArray.count); // returns two. 
    	NSLog(@"%@", theArray); // only prints two items 
    


    Thanks again! I have figured it out with your help! turns out I was removing the wrong objectAtIndex! i have been staring at the code for too long! :)
    My App site <a href="http://thetappyappco.com/" target="_blank">http://thetappyappco.com/</a>;
Sign In or Register to comment.