在 Objective c 中动态更改全局变量
Dynamically changing global variables in Objective c
如何设置一个在所有类中都可以访问的全局bool变量?它的值可以根据运行时条件更改。
在 AppDelegate.h
中声明您的 bool 变量
@property(nonatomic, assign)BOOL *isBOOL;
然后在你的view controller实现文件中创建app delegate共享应用对象,并在viewdidload中指定为共享应用对象。
@interface YourViewController ()
{
AppDelegate *appdel;
}
- (void)viewDidLoad {
appdel=(AppDelegate *)[[UIApplication sharedApplication] delegate];
}
现在您可以通过创建 appdelegate 共享应用程序对象在每个 class 中访问那个 isBOOL 变量,并且您还可以根据您的条件更改变量值。
appdel.isBOOL=YES/NO;
虽然@MiteshDobareeya 提出的 属性 单例 方法是一个有用的解决方案并且解决了同样的问题,但它实际上不是 全局的变量.
一个全局变量是一个每个人都可以访问的顶级变量:
Foo.h:
extern SomeType globalVariableName;
// For example:
extern NSString * globalString;
extern BOOL globalBool;
Foo.m
// This is at the top-level, outside any @implementation !
// You need this once in your app to actually define the
// variables values.
SomeType globalVariableName = initialValue;
NSString * globalString = @"Zoobar";
BOOL globalBool = YES;
SomeOther.m
#import "Foo.h"
- (void)foo {
// You can access the variable in any Objective-C method
// or even C function.
if (globalBool) {
[self doSomething];
}
}
我绝对不会采用任何建议的解决方案,这些解决方案基于纯 ANSI-C 解决方案或逻辑上不正确地推荐使用 app-delegate 作为全局模型,但我会在你的模型层上创建一个 singleton class,它可以在运行时为你存储任何 属性。
即使这在技术上不是 全局变量 – 它在任何项目中都比应用 纯 全局变量更有意义在一些随机 class 中,它并不真正属于实际的全局变量。
RuntimeEnvironment.h
@interface RuntimeEnvironment : NSObject
+ (instancetype)sharedEnvironment;
@property (nonatomic, getter=isSwitchOn) BOOL switch;
@end
RuntimeEnvironment.m
@implementation RuntimeEnvironment
+ (instancetype)sharedEnvironment {
static id _sharedInstance = nil;
@synchronized([self class]) {
if (_sharedInstance == nil) {
_sharedInstance = [[[self class] alloc] init];
}
}
return _sharedInstance;
}
@end
在行动:
设置变量:
[RuntimeEnvironment sharedEnvironment].switch = TRUE;
或读取其当前值:
BOOL _myGlobalSwitch = [RuntimeEnvironment sharedEnvironment].isSwitchOn;
随着项目的增长(它会),这个解决方案很容易维护或扩展,而且你也在一个地方正确地封装了所有东西,在你的模型上。
如何设置一个在所有类中都可以访问的全局bool变量?它的值可以根据运行时条件更改。
在 AppDelegate.h
中声明您的 bool 变量@property(nonatomic, assign)BOOL *isBOOL;
然后在你的view controller实现文件中创建app delegate共享应用对象,并在viewdidload中指定为共享应用对象。
@interface YourViewController ()
{
AppDelegate *appdel;
}
- (void)viewDidLoad {
appdel=(AppDelegate *)[[UIApplication sharedApplication] delegate];
}
现在您可以通过创建 appdelegate 共享应用程序对象在每个 class 中访问那个 isBOOL 变量,并且您还可以根据您的条件更改变量值。
appdel.isBOOL=YES/NO;
虽然@MiteshDobareeya 提出的 属性 单例 方法是一个有用的解决方案并且解决了同样的问题,但它实际上不是 全局的变量.
一个全局变量是一个每个人都可以访问的顶级变量:
Foo.h:
extern SomeType globalVariableName;
// For example:
extern NSString * globalString;
extern BOOL globalBool;
Foo.m
// This is at the top-level, outside any @implementation !
// You need this once in your app to actually define the
// variables values.
SomeType globalVariableName = initialValue;
NSString * globalString = @"Zoobar";
BOOL globalBool = YES;
SomeOther.m
#import "Foo.h"
- (void)foo {
// You can access the variable in any Objective-C method
// or even C function.
if (globalBool) {
[self doSomething];
}
}
我绝对不会采用任何建议的解决方案,这些解决方案基于纯 ANSI-C 解决方案或逻辑上不正确地推荐使用 app-delegate 作为全局模型,但我会在你的模型层上创建一个 singleton class,它可以在运行时为你存储任何 属性。
即使这在技术上不是 全局变量 – 它在任何项目中都比应用 纯 全局变量更有意义在一些随机 class 中,它并不真正属于实际的全局变量。
RuntimeEnvironment.h
@interface RuntimeEnvironment : NSObject
+ (instancetype)sharedEnvironment;
@property (nonatomic, getter=isSwitchOn) BOOL switch;
@end
RuntimeEnvironment.m
@implementation RuntimeEnvironment
+ (instancetype)sharedEnvironment {
static id _sharedInstance = nil;
@synchronized([self class]) {
if (_sharedInstance == nil) {
_sharedInstance = [[[self class] alloc] init];
}
}
return _sharedInstance;
}
@end
在行动:
设置变量:
[RuntimeEnvironment sharedEnvironment].switch = TRUE;
或读取其当前值:
BOOL _myGlobalSwitch = [RuntimeEnvironment sharedEnvironment].isSwitchOn;
随着项目的增长(它会),这个解决方案很容易维护或扩展,而且你也在一个地方正确地封装了所有东西,在你的模型上。