Objective-c 外部变量的变化
请帮忙,我被 Objective-C 全局变量又名 extern 困住了! 中编写了一些全局变量
@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
我编写我的第一个 iOS 应用程序,它是一个游戏,因此为了保存我的进度,我在 Globals.h:和 Globals.m:
#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
:我确保初始化方法在应用程序启动时至少执行一次。在 MainViewController 中,它是我在 viewDidLoad 方法中的 RootViewController,我使用:
[GameInfo hasSave];
这是 GameInfo 类中的静态方法,看起来像:
+(BOOL)hasSave
{
return [[NSFileManager defaultManager] fileExistsAtPath: savePath];
}
当我跟踪它时,savePath 类似于:
/Users/Apple/Library/Application Support/iPhone Simulator/5.0/Applications/472969F1-3760-4C74-8C8D-29681F47F4CB/Documents/save.plist
但是然后我在同一视图中的 Touch up 上同时执行 IBAction看起来像:
if ([GameInfo hasSave])
{
[GameInfo load];
}
当我在方法 hasSave 中执行一步时,我有一些奇怪的 savePath 值:
ar.lproj
我的方法当然返回“否”。但它不仅发生在我的 savePath 中,而且发生在所有其他外部变量中。有时它们是正确的,但有时却不是。 我想也许问题出在我使用它们的地方,也许我忘记了要做的事情。请帮我。
PS:抱歉我的英语不好,还有一个很大的问题。
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:
@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
And Globals.m:
#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
I make asured that initialize method executes at least once as app launched. In MainViewController which is my RootViewController in viewDidLoad method I use:
[GameInfo hasSave];
Which is a static method in GameInfo class and look like:
+(BOOL)hasSave
{
return [[NSFileManager defaultManager] fileExistsAtPath: savePath];
}
And when I trace it the savePath is something like:
/Users/Apple/Library/Application Support/iPhone Simulator/5.0/Applications/472969F1-3760-4C74-8C8D-29681F47F4CB/Documents/save.plist
But then I execute IBAction on Touch up inside in the same view, in the same time which look like:
if ([GameInfo hasSave])
{
[GameInfo load];
}
And when I make a step in method hasSave I have some strange savePath value:
ar.lproj
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.S.: Sorry about my English and a huge quastion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
[[NSFileManager defaultManager] fileExistsAtPath: savePath];
将返回NO
因为存在savePath
但不存在该文件还没有创建它~~~~~~~~~~~~~~~~~~已编辑~~~~~~~~~~~~~~~~~~~~~
在 GameInfo.h 或 .m 中使用
extern NSString *savePath;
your
[[NSFileManager defaultManager] fileExistsAtPath: savePath];
will returnNO
as there exits thesavePath
but there doesn't exists the file as you haven't created it in~~~~~~~~~~~~~~~~~ edited ~~~~~~~~~~~~~~~~~~~~
use
extern NSString *savePath;
in your GameInfo.h or .m为了消除垃圾值,您应该在实例化中初始化全局变量:
您还需要确保分配给全局变量的任何对象都被显式保留(和释放)。 (你的样品中有残留吗?)
To eliminate junk values, you should initialize your global variables in their instantiation:
You also need to make sure that any objects assigned to global variables are explicitly retained (and released). (Was there any retain in your sample?)