简体   繁体   English

Objective-C:如何以全局方式访问字符串变量?

[英]Objective-C: How Can I Access String Variable As a Global?

I am new to iPhone development. 我是iPhone开发的新手。 I want to access a string variable in all the class methods, and I want to access that string globally. 我想在所有类方法中访问一个字符串变量,并且想全局访问该字符串。 How can I do this? 我怎样才能做到这一点?

Please help me out. 请帮帮我。

Leaving aside the issue of global variables and if they are good coding practice... 抛开全局变量以及它们是否是良好的编码实践的问题...

Create your string outside of any Objective-C class in a .m file in your project: 在项目的.m文件中的任何Objective-C类之外创建字符串:

NSString *myGlobalString = @"foo";

Then put the declaration in a header file that is included by every other file that wants to access your string: 然后,将声明放入要访问您的字符串的所有其他文件包含的头文件中:

extern NSString *myGlobalString;

OK, well I can't leave it entirely aside. 好吧,我不能完全搁置它。 Have you considered putting your "global" string somewhere else, perhaps inside your application delegate as a (possibly read-only) property? 您是否考虑过将“全局”字符串放置在其他位置,也许是在应用程序委托中作为(可能是只读的)属性?

The preferred methods for creating a global variable are: 创建全局变量的首选方法是:

  1. Create a singleton class that stores the variables as an attributes. 创建一个单例类,将变量存储为属性。
  2. Create a class that has class methods that return the variables. 创建一个具有返回变量的类方法的类。 Put the class interface in the universal header so all classes in the project inherit it. 将类接口放在通用标头中,以便项目中的所有类都继承它。

The big advantage of method (2) is that it is encapsulated and portable. 方法(2)的最大优点是它是封装的和可移植的。 Need to use classes that use the global in another project? 是否需要在其他项目中使用使用全局变量的类? Just move the class with the variables along with them. 只需将带有变量的类与变量一起移动即可。

You can achieve that by implementing getter and setters in the delegate class. 您可以通过在委托类中实现getter和setter来实现。

In delegate .h file 在委托.h文件中

Include UIApplication delegate 包括UIApplication委托

 @interface DevAppDelegate : NSObject <UIApplicationDelegate>

  NSString * currentTitle;

 - (void) setCurrentTitle:(NSString *) currentTitle;
 - (NSString *) getCurrentTitle; 

In Delegate implementation class .m 在委托实现类.m中

 -(void) setCurrentLink:(NSString *) storydata{
currentLink = storydata;

}

-(NSString *) getCurrentLink{
if ( currentLink == nil ) {
    currentLink = @"Display StoryLink";
}
return currentLink;
}

So the variable you to assess is set in the currentlink string by setters method and class where you want the string ,just use the getter method. 因此,您要评估的变量是通过setters方法和要在其中放置字符串的类在currentlink字符串中设置的,只需使用getter方法即可。

All the best 祝一切顺利

I posted an article about my methodology for doing this: 我发表了一篇有关我的方法的文章:

http://www.pushplay.net/2011/02/quick-tip-global-defines/ http://www.pushplay.net/2011/02/quick-tip-global-defines/

This is something I primarily use for notification keys. 这是我主要用于通知键的东西。 Creating a globals.h file and adding it to the (your_project_name)_Prefix.pch file will ensure it is accessible globally... 创建一个globals.h文件并将其添加到(your_project_name)_Prefix.pch文件中,将确保可以全局访问该文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM