2013-10-08 47 views
2

我正在做一個小的簡單應用程序,所以我決定使用全局變量而不是單例。我也只使用一個。全局變量的鏈接器錯誤

我的應用程序從一個小的首選項文件中提取一個int,並將其作爲NSInteger設置爲全局變量。全局變量可能會在應用程序運行時更改。

AppController.h

#import <Cocoa/Cocoa.h> 

extern NSInteger preferenceNumber; 

@interface .... 

應用Controller.m或者

-(void)someMethod { 
    ... 
    //fileContents is a string containing the int that is inside the file 
    preferenceNumber = [fileContents intValue] 
    ... 
} 

鏈接器錯誤(2):用於建築x86_64的 未定義符號: 「_preferenceNumber」,從引用: - AppController.o中的[AppController someMethod1] - AppController.o中的[AppController someMethod2:] LD:符號(多個)未找到架構x86_64的 鐺:錯誤:連接命令,退出代碼1失敗(使用-v看調用)

加粗的部分是兩個錯誤。

這是什麼造成的?什麼是解決它的最簡單,最好的方法?

+0

凡是'preferenceNumber'的定義是什麼? – Sebastian

+0

@Sebastian對不起,它在那裏,我忘記把它變成代碼。它在應用程序Controller.m我編輯回 –

+2

仍然沒有定義,只有'preferenceNumber'聲明。 – Sebastian

回答

0

只需添加一個行實現類: -

AppContollr.m 


@implementation AppContoller 
NSInteger preferenceNumber;