如何在Objective-C中创建和使用队列?

如何在Objective-C中创建和使用队列?,第1张

概述我想在我的Objective-C程序中使用队列数据结构。在C我将使用STL队列。 Objective-C中的等效数据结构是什么?如何推/d出项目? Ben的版本是一个堆栈而不是一个队列,所以我调整了一点: NSMutableArray QueueAdditions.h @interface NSMutableArray (QueueAdditions)- (id) dequeue;- (voi 我想在我的Objective-C程序中使用队列数据结构。在C我将使用STL队列。 Objective-C中的等效数据结构是什么?如何推/d出项目?解决方法 Ben的版本是一个堆栈而不是一个队列,所以我调整了一点:

NSMutableArray QueueAdditions.h

@interface NSMutableArray (QueueAdditions)- (ID) dequeue;- (voID) enqueue:(ID)obj;@end

NSMutableArray QueueAdditions.m

@implementation NSMutableArray (QueueAdditions)// Queues are first-in-first-out,so we remove objects from the head- (ID) dequeue {    // if ([self count] == 0) return nil; // to avoID raising exception (Quinn)    ID headObject = [self objectAtIndex:0];    if (headObject != nil) {        [[headObject retain] autorelease]; // so it isn't dealloc'ed on remove        [self removeObjectAtIndex:0];    }    return headObject;}// Add to the tail of the queue (no one likes it when people cut in line!)- (voID) enqueue:(ID)anObject {    [self addobject:anObject];    //this method automatically adds to the end of the array}@end

只要导入.h文件,你想要使用你的新方法,并像你会像任何其他NSMutableArray方法调用它们。

祝你好运,继续编码!

总结

以上是内存溢出为你收集整理的如何在Objective-C中创建和使用队列?全部内容,希望文章能够帮你解决如何在Objective-C中创建和使用队列?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1049125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存