【发布时间】:2017-10-27 18:34:09
【问题描述】:
我想实现一个具有简单线程安全的 FIFO 队列类。我不想使用标准 Objective-C 框架中的任何类(NSObject 除外),这意味着我不想使用 NSMutableArray。
你能确定我的解决方案是否正确吗?谢谢!
#import <Foundation/NSObject.h>
@interface Queue : NSObject
{
id _value;
Queue *tail;
}
/* Puts an object at the end of the queue. The object is retained. */
- (void) putObject: (id)object;
/* Gets an object from the beginning of the queue. The object is
* removed from the queue. If there are no objects in the queue,
* returns nil. The object is autoreleased.
*/
- (id) getObject;
@end
@implementation Queue
- (id) init
{
return self;
}
- (void) dealloc
{
[super dealloc];
}
- (void) putObject: (id)object
{
if(tail)
{
[tail putObject:object];
}else{
tail = [[Queue alloc]init];
_object = object;
}
}
- (id) getObject
{
return _value;
}
@end
【问题讨论】:
-
Q 超出了 SO 的范围。
-
阅读他们的question guidelines 后,您可能希望将其移至代码审查。
标签: objective-c queue