Use NSMutableArray as a queue
Sometimes you might need to use a queue on your iPhone or iPad app. There are many ways to accomplish this task and I would like to discuss one of the ways we are using here at Walden Systems, because we believe this is the most straightforward method to create a queue.
Strategy to using a NSMutableArray as a queue is fairly simple: First, we need use categories on NSmutableArray by creating a new .h and .m file. Second, we need to declare 2 methods, one for adding to the top and one for deleting from the bottom of the queue. Third, we need to implement the method for add and remove. Finally, we can use the new methods we created for NSMutableArray.
Here are actual code samples:
The first thing you need to do is create the .h and .m file. To do this in Xcode, do the following:
1. Right-Click on your project and select New File. 2. Select Cocoa Touch class and click on Next. 3. Give it a class name and select NSObject as the subclass and click on Next.
Then you need to make changes to the .h file by deleting everything on the .h file and replacing it with the following to declare the methods :
1 #import2 3 @interface NSMutableArray ( Queue ) 4 - ( id ) deleteFromBottom ; 5 - ( void ) addToTop : ( id ) p_obj ; 6 7 @end
Then you will have implement the new methods by opening up the .m file and making the following changes :
1 #import "YOUR_CLASS_FILE.h" 2 3 @implementation NSMutableArray ( Queue ) 4 5 - ( id ) deleteFromBottom 6 { 7 @synchronized ( self ) 8 { 9 if ( [ self count ] == 0 ) 10 { 11 return nil ; 12 } 13 14 id queueObject = [ self objectAtIndex : 0 ] ; 15 16 [ self removeObjectAtIndex : 0 ] ; 17 18 return queueObject ; 19 } 20 } 21 - ( void ) addToTop : ( id ) obj 22 { 23 @synchronized ( self ) 24 { 25 [ self addObject : obj ] ; 26 } 27 } 28 29 @end 30 31
32 33 So, this is how the final code will look like: 34
35 Queue.h : 36
3738 #import 39 40 @interface NSMutableArray ( Queue ) 41 - ( id ) deleteFromBottom ; 42 - ( void ) addToTop : ( id ) p_obj ; 43 44 @end
Queue.m :
1 #import "YOUR_CLASS_FILE.h" 2 3 @implementation NSMutableArray ( Queue ) 4 5 - ( id ) deleteFromBottom 6 { 7 @synchronized ( self ) 8 { 9 if ( [ self count ] == 0 ) 10 { 11 return nil ; 12 } 13 14 id queueObject = [ self objectAtIndex : 0 ] ; 15 16 [ self removeObjectAtIndex : 0 ] ; 17 18 return queueObject ; 19 } 20 } 21 - ( void ) addToTop : ( id ) obj 22 { 23 @synchronized ( self ) 24 { 25 [ self addObject : obj ] ; 26 } 27 } 28 29 @end 30 31
32 33 To use the category, import the .h file in your class by adding the following line : 34 import "Queue.h" 35
36 Now when you want to use a NSMutableArray as a queue, you can simply do the following to declare the queue: 37
3839 NSMutableArray * queueArray ; 40 41 queueArray - [ [ NSMutableArray alloc ] init ] ;
To add the the queue, do the following :
[ queueArray addToTop: @"SOME_VALUE" ] ;
To delete from the queue, do the following :
1 NSString * someValue ; 2 3 someValue = [ queue deleteFromBottom ] ;