Objective C - 在头文件中使用typedef结构

时间:2014-02-16 09:25:25

标签: objective-c c linked-list

我正在尝试在Objective C中创建一个LinkedList。

在.h文件中,我尝试使用代码创建节点:

@interface AALinkedList : NSObject
{
    typedef struct Node
    {
        int data;
        struct Node *next;
    } Node;
}

这给我一个错误Type name does not allow storage class to be specified

这是什么意思?以及如何修复它?

2 个答案:

答案 0 :(得分:4)

typedef struct Node {
    int data;
    Node *next;
} Node;

@interface AALinkedList : NSObject
{
    Node node;
    // or Node *node;
}

答案 1 :(得分:0)

您无法在.h文件中声明typedef。在.m中声明它。这应该让你去。在.h

上放置方法签名

您可以像这样创建一个简单的LinkedList类。

@interface LinkedNode : NSObject 
  @property (nonatomic, strong) id nextNode;
@end
then you use it as you would expect:

id currentNode = myFirstNode;
do {
  [currentNode someMessage];
}
while(currentNode = currentNode.nextNode);