2015-04-05 119 views
0

剛剛開始在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]; 
} 

任何想法? 謝謝。

+0

瞭解如何獲取異常堆棧跟蹤,以確定故障點。 – 2015-04-05 19:46:33

回答

0

您沒有@implementation需要Instruction。在開始嘗試使用i變量之前,您也不會創建實例。

我不知道queuePush是什麼但也可能會導致您的問題...

+0

好吧,我假設這是我如何創建一個實例:Instruction * i = [[Instruction alloc] init]; 你能告訴我什麼頭文件想用@implementation? – s123 2015-04-05 16:59:35

+1

實際上並不清楚你沒有用於'Instruction'的'@ implementation'。顯示的兩個文件是Instruction.h和ViewController.m。 'Instruction'的實現通常在Instruction.m中,但沒有顯示。假設存在並且存在正常的東西,那麼未能創建'Instruction'實例並使'i'指向它可能是唯一的問題。而且,是的,你在上面的評論中顯示的片段是你將如何實例化一個'Instruction'實例。 – 2015-04-05 19:35:07

+0

謝謝。 我沒有Instruction.m 是否需要Instruction.m?我不打算在教學中有任何功能。只是實例變量。 – s123 2015-04-06 17:19:00