10

In Java there is a nice library class java.util.Stack which implements push and pop methods. Is there anything similar in Objective C?

I found only MutableArray. Seems weird I'd have to implement such a basic thing as a stack, there must be something like "NSStack", "NSQueue" and other similar stuff.

iseeall
  • 3,381
  • 9
  • 34
  • 43
  • possible duplicate of [Does the iOS SDK provide queues and stacks?](http://stackoverflow.com/questions/3652709/does-the-ios-sdk-provide-queues-and-stacks) – mattjgalloway Jun 27 '12 at 08:13

4 Answers4

24

Nobody should forget Objective-C offers a pretty nice variant: Objective-C++. And the C++ standard library provides the data structures you need, well tested, debugged, stable, and as fast as possible. Best, they'll work with ARC perfectly. Bestest, you can even choose between __weak or __strong pointers if you feels it.

Have a look at <queue> and <stack>.

That said, NSMutableArray works perfectly well for stacks: -addObject:, -lastObject and -removeLastObject will do the job nicely with good performance.

C++ can be verbose. Horribly verbose. But it also have some elegance here and there, and some very powerful constructs. Some parts of the standard library truly shine, and the data structures are among the pearls once the alien syntax is mastered. It can be hidden with a few typedefs anyway.

  • 2
    No but a tiny lil code example of how to use a from an objective-C .m file wouldn't hurt ;) – xaphod Jul 27 '15 at 09:09
  • There is one issue - C++ classes are available only in .mm files, and as soon as I rename my .m to .mm, XCode 8 compiler suddenly starts spamming me with linking errors for some headers I'm using, e.g. `Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_IOBluetoothRFCOMMChannel", referenced from: objc-class-ref in BluetoothRfcommPort.o` So, sometimes it's not an option to go Obj-C++, Unless I'm missing something. – JustAMartin Feb 28 '17 at 11:06
12

I've got a stack implementation here. It uses NSMutableArray to do the dirty work which really isn't that bad. But there is nothing built in to Foundation.

mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
  • 1
    Yep, too true! Glad you found it useful. Basically - you probably don't need a queue or a stack, just use an `NSMutableArray` and you'll be fine. – mattjgalloway Jun 27 '12 at 08:52
4

You (like myself) may worry about the dequeue performance of using NSMutableArray, because in dequeue you have to remove the first object in the NSMutableArray, and the removal will cause shifting of all the objects in the array. That is not necessary however based on the test I did here: for an NSMutableArray containing 100000 objects, remove all objects by continuously removing the first object is 100ms slower than by continuously removing the last object. I also compared using one NSMutableArray with using two NSMutableArrays. Although it's possible to avoid removing first object by using two NSMutableArrays which are used as two stacks, the double stack solution is actually slower.

jack
  • 731
  • 7
  • 8
1

as far as i know, there's nothing like stack and queue in the sdk.
there's an example for queue implementarion here.

Community
  • 1
  • 1
Redlil
  • 116
  • 8