如何在Objective c中声明和更改全局变量

时间:2023-01-15 14:49:47

can anyone tell how to declare and change global variables in objective c

任何人都可以告诉如何在目标c中声明和更改全局变量

4 个解决方案

#1


0  

Single source file:

单源文件:

int myGlobal;

Header file:

extern int myGlobal;

Any file including the header:

包含标题的任何文件:

myGlobal = 10;

#2


3  

Just the same way that you would in C. Are you having any particular problem?

就像你在C中一样。你有什么特别的问题吗?

#3


2  

On a related note; global variables are (very) generally speaking considered a Bad Thing™. In Obj-C the more common approach is making them a property on a singleton object, ensuring at least some encapsulation is taking place.

在相关的说明;全局变量(通常)被认为是Bad Thing™。在Obj-C中,更常见的方法是将它们作为单个对象的属性,确保至少进行一些封装。

In an AppKit/UIKit application; a global variable might more properly be a property on your application delegate; another, somewhat more involved, option is making a singleton class for encapsulating the variable and related methods.

在AppKit / UIKit应用程序中;全局变量可能更适合作为应用程序委托的属性;另一个更有意义的选择是创建一个单例类来封装变量和相关方法。

#4


2  

Global Variable for Complete iPhone Project

完整iPhone项目的全局变量

For Declare/Define/Use a global variable follow these easy steps:-

对于声明/定义/使用全局变量,请遵循以下简单步骤: -

  1. Create a NSObject File with named "GlobalVars.h and .m" or as u wish
  2. 创建一个名为“GlobalVars.h和.m”的NSObject文件,或者您希望的

  3. Declare your global variable in GlobalVars.h file after #import and before @implementation like-

    在#import之后和@implementation之前在GlobalVars.h文件中声明你的全局变量 -

    extern NSString *Var_name;

    extern NSString * Var_name;

  4. initialize it in GlobalVars.m file after #import and before @implementation like-

    在#import之后和@implementation之前在GlobalVars.m文件中初始化它 -

    NSString *Var_name = @"";

    NSString * Var_name = @“”;

  5. Define its property in AppDelegate.h File

    在AppDelegate.h文件中定义其属性

    @property (nonatomic, retain) NSString *Var_name;

    @property(nonatomic,retain)NSString * Var_name;

  6. synthesize it in AppDelegate.m File like-

    在AppDelegate.m文件中合成它像 -

    @synthesize Var_name;

  7. Now where you want to use this variable (in .m file) just import/inclue GlobalVars.h file in that all .h files, and you can access or can changes easily this variable.

    现在,您想要使用此变量(在.m文件中)只需在所有.h文件中导入/包含GlobalVars.h文件,您可以访问或轻松更改此变量。

  8. Carefully follow these Steps and it'll work Surely.
  9. 仔细按照这些步骤操作,它肯定会起作用。

#1


0  

Single source file:

单源文件:

int myGlobal;

Header file:

extern int myGlobal;

Any file including the header:

包含标题的任何文件:

myGlobal = 10;

#2


3  

Just the same way that you would in C. Are you having any particular problem?

就像你在C中一样。你有什么特别的问题吗?

#3


2  

On a related note; global variables are (very) generally speaking considered a Bad Thing™. In Obj-C the more common approach is making them a property on a singleton object, ensuring at least some encapsulation is taking place.

在相关的说明;全局变量(通常)被认为是Bad Thing™。在Obj-C中,更常见的方法是将它们作为单个对象的属性,确保至少进行一些封装。

In an AppKit/UIKit application; a global variable might more properly be a property on your application delegate; another, somewhat more involved, option is making a singleton class for encapsulating the variable and related methods.

在AppKit / UIKit应用程序中;全局变量可能更适合作为应用程序委托的属性;另一个更有意义的选择是创建一个单例类来封装变量和相关方法。

#4


2  

Global Variable for Complete iPhone Project

完整iPhone项目的全局变量

For Declare/Define/Use a global variable follow these easy steps:-

对于声明/定义/使用全局变量,请遵循以下简单步骤: -

  1. Create a NSObject File with named "GlobalVars.h and .m" or as u wish
  2. 创建一个名为“GlobalVars.h和.m”的NSObject文件,或者您希望的

  3. Declare your global variable in GlobalVars.h file after #import and before @implementation like-

    在#import之后和@implementation之前在GlobalVars.h文件中声明你的全局变量 -

    extern NSString *Var_name;

    extern NSString * Var_name;

  4. initialize it in GlobalVars.m file after #import and before @implementation like-

    在#import之后和@implementation之前在GlobalVars.m文件中初始化它 -

    NSString *Var_name = @"";

    NSString * Var_name = @“”;

  5. Define its property in AppDelegate.h File

    在AppDelegate.h文件中定义其属性

    @property (nonatomic, retain) NSString *Var_name;

    @property(nonatomic,retain)NSString * Var_name;

  6. synthesize it in AppDelegate.m File like-

    在AppDelegate.m文件中合成它像 -

    @synthesize Var_name;

  7. Now where you want to use this variable (in .m file) just import/inclue GlobalVars.h file in that all .h files, and you can access or can changes easily this variable.

    现在,您想要使用此变量(在.m文件中)只需在所有.h文件中导入/包含GlobalVars.h文件,您可以访问或轻松更改此变量。

  8. Carefully follow these Steps and it'll work Surely.
  9. 仔细按照这些步骤操作,它肯定会起作用。