你如何在 Xcode 中声明一个全局变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29006201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you declare a global variable in Xcode?
提问by cwiggo
I have just started to create my first iOs application with xcode.
我刚刚开始用 xcode 创建我的第一个 iOs 应用程序。
How can I initialise and access a variable from anywhere in the application?
如何从应用程序的任何地方初始化和访问变量?
For example. I want to pass a BASE url 'http://www.repetitiveurl.com'. to many of my added classes.
例如。我想传递一个 BASE url ' http://www.repetitiveurl.com'。我添加的许多课程。
回答by riyaz
declare a variable in any .h file (outside the @interface)
在任何 .h 文件中声明一个变量(@interface 之外)
and import the .h file in the class you wanted to use.
并在您要使用的类中导入 .h 文件。
回答by Shamas S - Reinstate Monica
Please see this answer.
请看这个答案。
This isn't a very straightforward way, but it is the right way.
这不是一个非常直接的方法,但它是正确的方法。
回答by LoVo
Both given answers are correct, i just add my answer to give another option to solve your "problem":
两个给出的答案都是正确的,我只是添加我的答案以提供解决您的“问题”的另一种选择:
You could declare the variable in the AppDelegate.h
, it depends on the variable and the use cases if this is a good idea. (usually it is not, but for some cases it is ok)
您可以在 中声明变量,AppDelegate.h
如果这是一个好主意,它取决于变量和用例。(通常不是,但在某些情况下是可以的)