2015-11-06 88 views
0

我想初始化全局變量,但是我不知道我在做什麼錯誤,因爲我得到一個錯誤。在Obj中初始化全局變量C

現在,我有一個Globals.h文件,我導入到我的AppDelegate中。在這裏,我聲明:

#ifndef Globals_h 
#define Globals_h 

#endif /* Globals_h */ 

extern NSArray *CompetencyOne; 
extern NSArray *CompetencyTwo; 
extern NSArray *CompetencyThree; 
extern NSArray *CompetencyFour; 
extern NSArray *CompetencyFive; 
extern NSArray *CompetencySix; 
extern NSArray *CompetencySeven; 
extern NSArray *CompetencyEight; 
extern NSArray *CompetencyNine; 
extern NSArray *CompetencyTen; 
extern NSArray *CompetencyEleven; 
extern NSArray *CompetencyTwelve; 
extern NSArray *Competencies; 

然後,我不知道在哪裏定義這些數組。我試着在AppDelegate.m在didFinishWithLaunchingOptions方法和視圖控制器的viewDidLoad方法,我創建了一個單獨的方法,

-(void) initializeCompetencies{ <arrays with values>} 
在視圖控制器

,但我不斷收到同樣的錯誤:

Undefined symbols for architecture x86_64: 
    "_Competencies", referenced from: 
    -[QuestionViewController viewDidLoad] in QuestionViewController.o 
    -[QuestionViewController changed:] in QuestionViewController.o 
    -[QuestionViewController nextMilestone:] in QuestionViewController.o 
    -[QuestionViewController pressBack:] in QuestionViewController.o 
    -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencyEight", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencyEleven", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencyFive", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencyFour", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencyNine", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencyOne", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencySeven", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencySix", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencyTen", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencyThree", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencyTwelve", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
    "_CompetencyTwo", referenced from: 
     -[QuestionViewController initializeCompetencies] in QuestionViewController.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

任何想法如何正確地聲明和初始化全局數組?或者,如何解決這個錯誤?謝謝!

回答

0

聲明和初始化是不夠的。您還必須定義的變量。

添加Globals.m文件和複製的聲明,消除extern

// Globals.m: 

NSArray *CompetencyOne; 
NSArray *CompetencyTwo; 
NSArray *CompetencyThree; 
... 
+0

謝謝。好吧,我有一個Globals。m文件的方法 - (void)initializeCompetencies {<數組定義>}。現在,我如何將這些信息傳遞給我的viewController? –

+0

您的Globals.m應該只是在類的頂部聲明數組,例如 NSArray * CompetencyOne = @ {....}; – davbryn

0

的sooo,數組是棘手的,因爲他們沒有編譯時間常數。你必須這樣做:

Constants.h

extern NSArray *myArray; 

Constants.m

NSArray *myArray = nil; 

然後,在某個應用程序初始化,可能是應用程序的委託去把啓動調用你的函數:

myArray = @[@1, @2, @3]; 
+0

感謝Logan的領導。澄清一下,在.h和.m中,我是在聲明和初始化數組? –

+0

直接在文件中聲明數組,然後初始化可能在從應用程序委託調用的全局函數中完成啓動 – Logan

0

這聽起來更像是你想要一個對象來創建im可變數組在應用程序內消耗。

怎麼樣叫CompentencyFactory類有一堆的靜態方法:

+ (NSArray *) competencyOne; 
+ (NSArray *) competencyTwo; 
看起來像這樣

等:

+ (NSArray *) competencyOne { 
    return @[ 
     @"foo", 
     @"bar", 
     @"fubnut" 
    ]; 
} 

,你可以在整個應用程序引用類似:

NSArray *comp = [CompetencyFactory competencyOne]; 
2

Objective-C對象是,除了NSString常量S,只有在運行時所創造,所以你不能使用表達式,否則初始化它們,你會得到錯誤:初始元素不是一個編譯時間常數

如果聲明:

//Globals.h 
extern NSArray *CompetencyOne; 

//Globals.m 
NSArray *CompetencyOne = nil; //this works coz nil is compile time constant 

請檢查出link

+0

實際上不需要'= nil'因爲所有的「靜態存儲持續時間」對象都被初始化爲零。這是來自C標準。 –