我想在Objective-C程序中使用队列数据 struct .在C++中,我使用STL队列.Objective-C中的等效数据 struct 是什么?如何推送/弹出项目?

推荐答案

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相关问答推荐

在 iOS 中检测 PAN 手势的方向

UIDatePicker,根据今天的日期设置最大和最小日期

不区分大小写的核心数据 CONTAINS 或 BEGINS WITH 约束

UITableView - Select 了哪一行?

在 iOS 7 中检测后台应用刷新的用户设置

当单元格全屏时,为什么 UICollectionView 会记录错误?

iOS 6 Facebook 发布过程以remote_app_id 与存储的 id 不匹配结束

Objective-C:如何从子类访问父属性?

如何测试生产推送通知?

如何删除手势识别器

ios 11 UITabBar UITabBarItem 定位问题

将块存储在数组中

从 UIImage 获取 Exif 数据 - UIImagePickerController

iPhone:在 UITextField 上禁用 Auto-Cap/autocorrect 问题

Cocoapods ld:找不到-lPods-Projectname 的库

iOS 11 large-title导航栏不折叠

导航返回时重新加载 UITableView?

如何判断数组是空还是空?

iPhone如何在打字时获取UITextField的文本?

如何调整 UIToolBar 左右内边距