Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use NSMutableArray as a queue?

Tags:

cocoa

I have NSMutableArray and used it like queue for waiting actions.

Example in array:

0: "Do something"
1: "Do something else"
2: "Do something 2"

When I used [myarray removeObjectAtIndex:0] the array is not reordering and next time when I use [myarray objectAtIndex:o] the result is nil.

How can I put "Do something else" in the first index and "Do something 2" in the second index when I remove "Do something"?

like image 261
Shyne Avatar asked Jun 30 '26 23:06

Shyne


1 Answers

/* gcc -framework Cocoa myprogram.m -o myprogram */

#import <Cocoa/Cocoa.h>

int main( int argc, char **argv )
{
  NSMutableArray *array = [ [ NSMutableArray alloc ] 
                           initWithObjects: @"1", @"2", @"3",
                                            nil ]; /* don't forget nil */

  /* "pop" the first object */
  [ array removeObjectAtIndex:0 ];

  /* prints "2" as expected */
  NSLog( @"%@", [ array objectAtIndex: 0 ] );
}
like image 118
codelogic Avatar answered Jul 04 '26 07:07

codelogic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!