指向接口对象的指针会产生分段错误

时间:2015-04-05 16:42:45

标签: objective-c

刚开始用Objective C编程......

此代码给我一个分段错误。它出什么问题了? 我知道这肯定与“指令”有关。阵列工作正常。

Instruction.h:

    #ifndef romo_objectice_c_Instruction_h
    #define romo_objectice_c_Instruction_h
    #endif

    #import <Foundation/Foundation.h>
    typedef enum direction {north, south, west, east} Direction;
    @interface Instruction:NSObject
        @property Direction dir;
        @property unsigned int distance;
    @end

ViewController.m:

    #import "Instruction.h"
    #import "ViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    @synthesize instructionList;

    - (void)viewDidLoad {
    [super viewDidLoad];
    instructionList = [[NSMutableArray alloc] init];
    //THE FOLLOWING CODE CAUSES THE SEGMENTATION FAULT
    Instruction *i;
    Direction d = north;
    i.distance = 1;
    i.dir = d;
    [instructionList queuePush:i];
}

有什么想法吗? 感谢。

1 个答案:

答案 0 :(得分:0)

您需要的@implementation没有Instruction。在开始尝试使用i变量之前,您也不会创建实例。

我不确定queuePush是什么,但这也可能导致问题......