├── NSMutableArray+QueueAdditions.h ├── NSMutableArray+QueueAdditions.m ├── README.md └── snip.m /NSMutableArray+QueueAdditions.h: -------------------------------------------------------------------------------- 1 | /* 2 | Generic queue. 3 | */ 4 | #import 5 | 6 | @interface NSMutableArray (QueueAdditions) 7 | 8 | -(id) dequeue; 9 | -(void) enqueue:(id)obj; 10 | -(id) peek:(int)index; 11 | -(id) peekHead; 12 | -(id) peekTail; 13 | -(BOOL) empty; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /NSMutableArray+QueueAdditions.m: -------------------------------------------------------------------------------- 1 | #import "NSMutableArray+QueueAdditions.h" 2 | 3 | @implementation NSMutableArray (QueueAdditions) 4 | 5 | // Add to the tail of the queue 6 | -(void) enqueue: (id) anObject { 7 | // Push the item in 8 | [self addObject: anObject]; 9 | } 10 | 11 | // Grab the next item in the queue, if there is one 12 | -(id) dequeue { 13 | // Set aside a reference to the object to pass back 14 | id queueObject = nil; 15 | 16 | // Do we have any items? 17 | if ([self lastObject]) { 18 | // Pick out the first one 19 | #if !__has_feature(objc_arc) 20 | queueObject = [[[self objectAtIndex: 0] retain] autorelease]; 21 | #else 22 | queueObject = [self objectAtIndex: 0]; 23 | #endif 24 | // Remove it from the queue 25 | [self removeObjectAtIndex: 0]; 26 | } 27 | 28 | // Pass back the dequeued object, if any 29 | return queueObject; 30 | } 31 | 32 | // Takes a look at an object at a given location 33 | -(id) peek: (int) index { 34 | // Set aside a reference to the peeked at object 35 | id peekObject = nil; 36 | // Do we have any items at all? 37 | if ([self lastObject]) { 38 | // Is this within range? 39 | if (index < [self count]) { 40 | // Get the object at this index 41 | peekObject = [self objectAtIndex: index]; 42 | } 43 | } 44 | 45 | // Pass back the peeked at object, if any 46 | return peekObject; 47 | } 48 | 49 | // Let's take a look at the next item to be dequeued 50 | -(id) peekHead { 51 | // Peek at the next item 52 | return [self peek: 0]; 53 | } 54 | 55 | // Let's take a look at the last item to have been added to the queue 56 | -(id) peekTail { 57 | // Pick out the last item 58 | return [self lastObject]; 59 | } 60 | 61 | // Checks if the queue is empty 62 | -(BOOL) empty { 63 | return ([self lastObject] == nil); 64 | } 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #### Queue Object for iOS 2 | This is a simple addition to NSMutableArray which gives you a queue object. Use enqueue and dequeue to manage your first-in-first-out queue. It can handle any kind of object. 3 | 4 | _I found this code floating around on the internet, by no means am I claiming it for myself!!_ 5 | 6 | Example: 7 | 8 | // add 3 objects 9 | [q enqueue:@"object1"]; 10 | [q enqueue:@"object2"]; 11 | [q enqueue:@"object3"]; 12 | 13 | // print them 14 | NSLog(@"%@", [q dequeue] ); 15 | NSLog(@"%@", [q dequeue] ); 16 | NSLog(@"%@", [q dequeue] ); 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /snip.m: -------------------------------------------------------------------------------- 1 | //--Global import-- 2 | #import "NSMutableArray+QueueAdditions.h" 3 | 4 | 5 | 6 | //--Inside some function-- 7 | 8 | // add 3 objects 9 | [q enqueue:@"object1"]; 10 | [q enqueue:@"object2"]; 11 | [q enqueue:@"object3"]; 12 | 13 | [q peekHead]; 14 | [q peekTail]; 15 | [q peek:1]; 16 | 17 | // print them 18 | NSLog(@"%@", [q dequeue] ); 19 | NSLog(@"%@", [q dequeue] ); 20 | NSLog(@"%@", [q dequeue] ); 21 | --------------------------------------------------------------------------------