在不同 类 中创建和访问 "debugMode" 变量
Create and access "debugMode" variable across different classes
我正在尝试创建一个布尔变量 debugMode,并在多个不同的 classes 中访问它。这样我就可以在我的 ViewController 中设置它的值一次,并且能够在我的不同 classes(SKScene 的子classes)中访问它以显示帧率,记录物理值等
我读到我需要创建我的 class 的实例?我看不出这在这个程序中是如何应用的。
我是 objective-c 的新手,非常感谢任何帮助!谢谢!
默认解决方案是预处理器定义,这在 xcode 个项目中默认设置。
因此,您可以在源代码中输入
#ifdef DEBUG
// code that should only run in Debug Configuration
#endif
所以如果我没看错你想要一个给定 class 的实例,你可以在整个应用程序中使用它而不会丢失 class 的状态,但这应该只存在于DEBUG
版本的代码?
好的,我们可以使用与 #ifdef DEBUG
混合的单例模式来确定是否处于调试模式。
DebugManager.h
// Our Debug Class that we have just made up.
// Singleton class
@interface DebugManager : NSObject
// Some properties that we want on the class
@property (nonatomic, strong) NSString *debugName;
@property (nonatomic, readonly) NSDate *instanceCreatedOn;
// a method for us to get the shared instance of our class
+ (id)sharedDebugManager;
@end
DebugManager.m
#import "DebugManager.h"
@implementation DebugManager
// Create a shared instance of our class unless one exists already
+ (id)sharedDebugManager
{
static DebugManager *sharedDebugManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedDebugManager = [[self alloc] init];
});
return sharedDebugManager;
}
- (id)init
{
if (self = [super init]) {
debugName = @"Debug Instance";
instanceCreatedOn = [NSDate new];
}
return self;
}
@end
现在我们有了单例 class 设置,我们可以将下面的行添加到我们的 *-Prefix.pch
中,这将为我们提供 DebugManager
class 的实例,我们可以在我们的整个应用程序中使用。
#ifdef DEBUG
DebugManager *manager = [DebugManager sharedDebugManager];
#endif
请记住,当您想使用 manager
实例时,您需要将其包装在 #ifdef DEBUG
中,因为当 运行 在生产中时,这将看不到 manager
了。所以一定要这样做:
#ifdef DEBUG
NSLog(@"The DebugManagers instance name is %@", [manager debugName]);
#endif
不要忘记在 Build Settings
下的 xcode 中添加预处理器宏,按照 this 的回答了解如何做到这一点
如果您有任何问题,请在下方提问。
我正在尝试创建一个布尔变量 debugMode,并在多个不同的 classes 中访问它。这样我就可以在我的 ViewController 中设置它的值一次,并且能够在我的不同 classes(SKScene 的子classes)中访问它以显示帧率,记录物理值等
我读到我需要创建我的 class 的实例?我看不出这在这个程序中是如何应用的。
我是 objective-c 的新手,非常感谢任何帮助!谢谢!
默认解决方案是预处理器定义,这在 xcode 个项目中默认设置。
因此,您可以在源代码中输入
#ifdef DEBUG
// code that should only run in Debug Configuration
#endif
所以如果我没看错你想要一个给定 class 的实例,你可以在整个应用程序中使用它而不会丢失 class 的状态,但这应该只存在于DEBUG
版本的代码?
好的,我们可以使用与 #ifdef DEBUG
混合的单例模式来确定是否处于调试模式。
DebugManager.h
// Our Debug Class that we have just made up.
// Singleton class
@interface DebugManager : NSObject
// Some properties that we want on the class
@property (nonatomic, strong) NSString *debugName;
@property (nonatomic, readonly) NSDate *instanceCreatedOn;
// a method for us to get the shared instance of our class
+ (id)sharedDebugManager;
@end
DebugManager.m
#import "DebugManager.h"
@implementation DebugManager
// Create a shared instance of our class unless one exists already
+ (id)sharedDebugManager
{
static DebugManager *sharedDebugManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedDebugManager = [[self alloc] init];
});
return sharedDebugManager;
}
- (id)init
{
if (self = [super init]) {
debugName = @"Debug Instance";
instanceCreatedOn = [NSDate new];
}
return self;
}
@end
现在我们有了单例 class 设置,我们可以将下面的行添加到我们的 *-Prefix.pch
中,这将为我们提供 DebugManager
class 的实例,我们可以在我们的整个应用程序中使用。
#ifdef DEBUG
DebugManager *manager = [DebugManager sharedDebugManager];
#endif
请记住,当您想使用 manager
实例时,您需要将其包装在 #ifdef DEBUG
中,因为当 运行 在生产中时,这将看不到 manager
了。所以一定要这样做:
#ifdef DEBUG
NSLog(@"The DebugManagers instance name is %@", [manager debugName]);
#endif
不要忘记在 Build Settings
下的 xcode 中添加预处理器宏,按照 this 的回答了解如何做到这一点
如果您有任何问题,请在下方提问。