iOS SDK 是否提供队列和堆栈?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3652709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:40:33  来源:igfitidea点击:

Does the iOS SDK provide queues and stacks?

stackqueueios

提问by Tommy Herbert

I'm writing an iPhone app, and I'm surprised that there seem to be no NSQueue or NSStack classes in Apple's Foundation Framework. I see that it would be quite easy to roll my own, starting with an NSMutableArray, so I'll do that unless I've missed something. Have I missed something?

我正在编写一个 iPhone 应用程序,我很惊讶 Apple 的Foundation Framework 中似乎没有 NSQueue 或 NSStack 类。我看到从NSMutableArray开始滚动我自己的会很容易,所以除非我错过了什么,否则我会这样做。我错过了什么吗?

采纳答案by Michael W.

as far as I know there is no generic class avaialbe. Try using the NSMutableArray, add via addObject and get first/last via objectAtIndex and removeObjectAtIndex.

据我所知,没有通用类avaialbe。尝试使用 NSMutableArray,通过 addObject 添加并通过 objectAtIndex 和 removeObjectAtIndex 获取第一个/最后一个。

回答by Tommy Herbert

Here's my Stack class, in case it's useful to those who come after me. As you can see, the pop method involves enough code that you'd want to factor it out.

这是我的 Stack 类,以防它对那些追随我的人有用。如您所见,pop 方法包含了足够多的代码,您需要将其分解。

Stack.h:

堆栈.h:

#import <Foundation/Foundation.h>

@interface Stack : NSObject {
    NSMutableArray *contents;
}

- (void)push:(id)object;
- (id)pop;

@end

Stack.m

堆栈

#import "Stack.h"

@implementation Stack

// superclass overrides

- (id)init {
    if (self = [super init]) {
        contents = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)dealloc {
    [contents release];
    [super dealloc];
}

// Stack methods

- (void)push:(id)object {
    [contents addObject:object];
}

- (id)pop {
    id returnObject = [[contents lastObject] retain];
    if (returnObject) {
            [contents removeLastObject];
    }
    return [returnObject autorelease];
}

@end

回答by occulus

I'm a bit late to this party, but are you aware of CHDataStructures?

我参加这个派对有点晚了,但是你知道 CHDataStructures 吗?

http://cocoaheads.byu.edu/code/CHDataStructures

http://cocoaheads.byu.edu/code/CHDataStructures

回答by Mischa

Another easy way would be to extend NSMutableArray's capabilities by making use of Objective C's categories. You can do that by adding two files to your project:

另一种简单的方法是NSMutableArray通过使用 Objective C 的类别来扩展的功能。您可以通过向项目中添加两个文件来实现:

NSMutableArray+Stack.h

NSMutableArray+Stack.h

@interface NSMutableArray (StackExtension)

- (void)push:(id)object;
- (id)pop;

@end

NSMutableArray+Stack.m

NSMutableArray+Stack.m

#import "NSMutableArray+Stack.h"

@implementation NSMutableArray (StackExtension)

- (void)push:(id)object {
    [self addObject:object];
}

- (id)pop {
    id lastObject = [self lastObject];
    [self removeLastObject];
    return lastObject;
}

@end

Now you can use a regular NSMutableArrayin every other file of your project like a stack and call pushor popon that object. Don't forget to #import NSMutableArray+Stack.hin those files. Here is some sample code how you can use your new NSMutableArrayas a stack:

现在,您可以NSMutableArray在项目的每个其他文件中使用常规,例如堆栈和调用pushpop在该对象上。不要忘记#import NSMutableArray+Stack.h在这些文件中。以下是一些示例代码,您可以如何将 newNSMutableArray作为堆栈使用:

NSMutableArray *myStack = [[NSMutableArray alloc] init]; // stack size = 0

NSString *aString = @"hello world";
[myStack push:myString];            // stack size = 1

NSString *anotherString = @"hello universe";
[myStack push:anotherString];       // stack size = 2

NSString *topMostStackObject; 

topMostStackObject = [myStack pop]; // stack size = 1
NSLog("%@",topMostStackObject);

topMostStackObject = [myStack pop]; // stack size = 0
NSLog("%@",topMostStackObject);

The log output will be:

日志输出将是:

hello universe
hello world

回答by portforwardpodcast

I have put a working iOS Objective C queue object on GitHub. The code was taken from various posts and by no means is owned by me.

我在 GitHub 上放置了一个可用的 iOS Objective C 队列对象。该代码取自各种帖子,绝不属于我。

https://github.com/esromneb/ios-queue-object/

https://github.com/esromneb/ios-queue-object/

If you see any problems please fork, and make a pull request!

如果您发现任何问题,请 fork,并提出请求请求!

回答by Gabe

ObjectiveSugaris a very popular CocoaPod that provides, among a bunch of other great stuff, pushand popAPI calls on NSMutableArray. Sure, it's not in the iOS SDK, but I'm sharing it here because I was looking for the same thing, and this was the solution I went with (and it certainly didn't hurt that we were already using this CocoaPod in our codebase).

ObjectiveSugar是一个非常流行的 CocoaPod,它提供了许多其他很棒的东西,push以及popNSMutableArray. 当然,它不在 iOS SDK 中,但我在这里分享它是因为我正在寻找相同的东西,这是我采用的解决方案(当然我们已经在我们的代码库)。

回答by kennytm

Yes, an NSMutableArray doubles as a stack or queue. (It would be slightly inefficient as a queue.)

是的,NSMutableArray 兼作堆栈或队列。(作为队列会稍微低效。)

You could also use C++'s stackand queueadapter, but it makes memory management a bit messy if you want to store Objective-C objects with it.

你也可以使用 C++stackqueue适配器,但是如果你想用它存储 Objective-C 对象,它会使内存管理有点混乱。

回答by Eonil

No. You missed nothing. That's all. Objective-C is higher level language look like C. Low level control is not required.

不,你什么都没错过。就这样。Objective-C的是高级语言的样子C.低电平控制不是必需的。

Cocoa classes are designed for easier use than efficiency. If you want to deal with performance, you have an option of raw C (or C++) implementation. Otherwise, just use easy way. Of course, early-optimization is evil.

Cocoa 类旨在更容易使用而不是效率。如果您想处理性能问题,您可以选择原始 C(或 C++)实现。否则,只需使用简单的方法。当然,早期优化是邪恶的。

If you want a kind of encapsulation, just make a new class which contains NSMutableArray within it. Hide inner NSMutableArray and just expose what you want. But you'll realize this is unnecessary.

如果你想要一种封装,只需创建一个包含 NSMutableArray 的新类。隐藏内部 NSMutableArray 并公开您想要的内容。但你会意识到这是不必要的。