可能重複:
How to use global variables in Objective-C?在Objective-C全局字符串變量
在我Login.m
文件,我有
NSString *userName = txtUsername.text; // this is getting the text from the username text field and storing it in userName
的問題是,我需要訪問來自不同類的變量userName,所以我需要將其聲明爲全局變量。這樣做的最好方法是什麼?
這個問題從[XY問題]遭受(http://meta.stackexchange.com/questions/ 66377 /)。至於它描述的解決方案,[globals are bad](http://c2.com/cgi/wiki?GlobalVariablesAreBad)。作爲替代方法,請參閱[Objective-C:在一個線程中進行分配並在其他版本中進行分配](http://stackoverflow.com/q/4698273/90527),以獲取帶訪問器的類變量的示例實現。 – outis