想知道創建手動數組的最佳方式,而不使用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? –