可能重复:
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