Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-c extern variables changes
    primarykey
    data
    text
    <p>Please help, I'm stuck with objective-c global variables aka extern! I writing my first iOS app it is a game, so to save my progress I wrote some global variables in Globals.h:</p> <pre><code>@interface Globals : NSObject extern float square; extern float field; extern float dx; extern float dy; extern NSURL *documentsUrl; extern NSURL *dbUrl; extern NSString *savePath; extern FMDatabase *db; @end </code></pre> <p>And Globals.m:</p> <pre><code>#import "Globals.h" @implementation Globals float square; float field; float dx; float dy; NSURL *documentsUrl; NSURL *dbUrl; NSString *savePath; FMDatabase *db; +(void)initialize { static BOOL initialized = NO; if (!initialized) { initialized = YES; square = 177.7778; field = 146.0; dx = 105; dy = 58.5; documentsUrl = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; dbUrl = [documentsUrl URLByAppendingPathComponent:@"alias.sql"]; [[NSFileManager defaultManager] copyItemAtURL:[[NSBundle mainBundle] URLForResource:@"alias" withExtension:@"sql"] toURL:dbUrl error:nil]; savePath = [[documentsUrl URLByAppendingPathComponent:@"save.plist"] path]; db = [FMDatabase databaseWithPath:[dbUrl path]]; //db.logsErrors = YES; //db.traceExecution = YES; if (![db open]) { NSLog(@"Could not open db."); } } } @end </code></pre> <p>I make asured that initialize method executes at least once as app launched. In MainViewController which is my RootViewController in viewDidLoad method I use:</p> <pre><code>[GameInfo hasSave]; </code></pre> <p>Which is a static method in GameInfo class and look like:</p> <pre><code>+(BOOL)hasSave { return [[NSFileManager defaultManager] fileExistsAtPath: savePath]; } </code></pre> <p>And when I trace it the savePath is something like:</p> <blockquote> <p>/Users/Apple/Library/Application Support/iPhone Simulator/5.0/Applications/472969F1-3760-4C74-8C8D-29681F47F4CB/Documents/save.plist</p> </blockquote> <p>But then I execute IBAction on Touch up inside in the same view, in the same time which look like:</p> <pre><code>if ([GameInfo hasSave]) { [GameInfo load]; } </code></pre> <p>And when I make a step in method hasSave I have some strange savePath value:</p> <blockquote> <p>ar.lproj</p> </blockquote> <p>And my method of course returns me NO. But it occurs not only with my savePath, but with all other extern variables. And sometimes they are correct but sometimes they are not. I think maybe the problem is where I use them, maybe I forgot something to do. Please help me.</p> <p>P.S.: Sorry about my English and a huge quastion.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload