0

I am trying to create a Boolean variable, debugMode, and access it across several different classes. This way I can set it's value once in my ViewController, and will be able to access it in my different classes (subclasses of SKScene) to show framerate, log physics values, etc.

I have read that I need to create an instance of my class? I don't see how that applies in this program.

I am new to objective-c and would greatly appreciate any help! Thank you!

picciano
  • 22,341
  • 9
  • 69
  • 82

2 Answers2

1

The default solution is a preprocessor define, this is set by default in xcode projects.

So, in the source you can put

#ifdef DEBUG
// code that should only run in Debug Configuration
#endif
OliverD
  • 864
  • 7
  • 8
-2

So if I get you right you want an instance of a given class that you can use across the whole of your application without losing the state of the class but this should only exist in the DEBUG version of your code?

Ok we can do this using a Singleton Pattern mixed with the #ifdef DEBUG to determine whether in debug mode or not.

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

Now that we have a Singleton class setup we can add the below line to our *-Prefix.pch which will give us an instance of our DebugManager class that we can use throughout our app.

#ifdef DEBUG
    DebugManager *manager = [DebugManager sharedDebugManager];
#endif

Just remember that when you want to use your manager instance you will need to wrap it in #ifdef DEBUG because when running in production this will not see the instance of manager anymore. So make sure you do:

#ifdef DEBUG
     NSLog(@"The DebugManagers instance name is %@", [manager debugName]);
#endif

Don't forget to add your preprocessor macro in xcode under your Build Settings follow this answer to find out how to do that

If you have any questions just ask below.

Community
  • 1
  • 1
Popeye
  • 11,839
  • 9
  • 58
  • 91
  • I think we're on the same page. Just to clarify, I would set the value of debugMode to YES at the launch of the application. Then when my application runs a particular scene, I could do something along the lines of "if (debugMode) {log the coordinate of the player, etc}". I understand the syntax would be much different. – Luigi Mangione Mar 02 '15 at 15:16
  • What you need to do to get the `DEBUG` variable is go to your `Build Settings` search for `Prepocessor Macros` and add `DEBUG=1` under just the `Debug` option **NOT** the release option. – Popeye Mar 02 '15 at 15:33
  • Got it. I just set DEBUG = 1, and then, as you said, used #ifdef in order to log certain messages. Thank you for the informative response and I apologize if I wasn't clear in the beginning. Also, how would I switch to the release version? – Luigi Mangione Mar 02 '15 at 16:03
  • To switch to your release version you need to change the scheme version to release. – Popeye Mar 02 '15 at 16:03
  • When I go to product > scheme, there is only one option (the name of my project). How would I change the version? Sorry, I'm new to Xcode. – Luigi Mangione Mar 02 '15 at 16:23