想知道创建手动数组的最佳方式,而不使用NSMutalbleArray,我一直在研究可能的最佳解决方案,但没有一个优雅的答案,您认为Objective C中的最佳方法是什么从头开始创建一个NSMutableArray样式对象?使用FIFO队列作为最终解决方案,即使是基本的数组结构也是一个很好的提示!谢谢, 约翰目标C - 手动排队FIFO队列
3
A
回答
14
NSMutableArray上的类别是IMO最简单的方法。我有栈(LIFO)和队列类别(FIFO)
页眉
#import <Foundation/Foundation.h>
@interface NSMutableArray (QueueStack)
-(id)queuePop;
-(void)queuePush:(id)obj;
-(id)stackPop;
-(void)stackPush:(id)obj;
@end
实施
#import "NSMutableArray+QueueStack.h"
@implementation NSMutableArray (QueueStack)
// Queues are first-in-first-out, so we remove objects from the head
-(id)queuePop {
@synchronized(self)
{
if ([self count] == 0) {
return nil;
}
id queueObject = [[[self objectAtIndex:0] retain] autorelease];
[self removeObjectAtIndex:0];
return queueObject;
}
}
// Add to the tail of the queue
-(void)queuePush:(id)anObject {
@synchronized(self)
{
[self addObject:anObject];
}
}
//Stacks are last-in-first-out.
-(id)stackPop {
@synchronized(self)
{
id lastObject = [[[self lastObject] retain] autorelease];
if (lastObject)
[self removeLastObject];
return lastObject;
}
}
-(void)stackPush:(id)obj {
@synchronized(self)
{
[self addObject: obj];
}
}
@end
制作和使用队列:
NSMutableArray *queue = [NSMutableArray array];
//Put an item in the queue
[queue queuePush:myObj];
//Retrieve an item, (this will be the first one)
MyCoolObject *myObject = [queue queuePop];
1
即使我不明白NSMutableArray的问题,这是一种使用双向链接来实现队列的方法编辑列表(希望我得到它的权利,我有点累了)):
注:我假设使用ARC。
//Node.h
@interface Node : NSObject
@property (strong)id value;
@property (strong)Node *previous;
@property (strong)Node *next;
//Node.m
@implementation
@end
/Queue.h
@interface Queue : NSObject
- (void)enqueue:(id)objectToEnqueue;
- (id)dequeue;
@end
//Queue.m
@interface Queue()
{
Node *start;
}
@implementation
- (void)enqueue:(id)objectToEnqueue
{
Node *node = [Node new];
node.value = objectToEnqueue;
if (nil == start)
{
node.previous = node;
node.next = node;
start = node;
}
else
{
node.previous = start.previous;
node.next = start;
start.previous = node;
start = node;
}
}
- (id)dequeue
{
if (nil == start)
return nil;
Node *node = start.previous;
start.previous = start.previous.previous;
start.previous.next = start;
id objectToDequeue = node.value;
return objectToDequeue;
}
@end
如果你正在寻找一种方式做,在纯C,也许这将帮助你:
+0
可爱的回答欢呼 – Woodstock
2
阵列与FIFO方式:
if (mutArr.count == 5) {
for (int j = 0; j < 4; j++) {
[mutArr exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
[mutArr removeObjectAtIndex:4];
[mutArr addObject:mutDict];
}else{
[mutArr addObject:mutDict];
}
相关问题
- 1. 我可以使用C++矢量插入FIFO队列排队
- 2. FIFO队列同步
- 3. Amazon Sqs FIFO队列
- 4. FIFO队列链表实现
- 5. 实现一个队列(fifo)
- 6. 在c中创建一个FIFO队列
- 7. 静态FIFO队列与计时器c#
- 8. Firebase中的FIFO队列
- 9. 原型JavaScript Ajax FIFO队列
- 10. Java FIFO队列实现
- 11. NSOperationQueue串行FIFO队列
- 12. FIFO队列显示问题
- 13. 排队并发队列
- 14. c#调度队列像在目标c
- 15. 入队C#队列
- 16. 作为“FIFO队列”的Javascript循环缓冲区队列实现
- 17. C,排序结构队列
- 18. 排序队列链表C++
- 19. 动态队列C++
- 20. AWS SQS标准队列或FIFO队列何时不能重复消息?
- 21. 使用一个或多个标准FIFO队列实现延迟队列
- 22. FIFO队列头指针不正确
- 23. C中的队列队列#
- 24. 基于变量重新排列Fifo队列
- 25. 排队列在Excel
- 26. 我自己的消息队列/ fifo
- 27. jquery鼠标手势/ btn排队
- 28. 排序队列
- 29. AWS Kinesis Stream作为FIFO队列
- 30. Java溢出到磁盘的FIFO队列
的任'id *'或基于链表的解决方案将工作... – 2012-10-29 20:37:21
NSMutableArray有什么问题? –
如果您需要绑定它,为什么不使用正常的数组并管理push和pop indeces? –