我已经研究了这个问题很长时间,但似乎找不到答案。我对 iPhone 编程还比较新手,如果这是一个愚蠢的问题,请原谅。如果有人可以提供具体的代码来展示如何实现,那将非常有帮助。
Objective-C是C语言的超集,所以只需按照C语言的方式进行操作:
int globalX = 100;
还有头文件:
extern int globalX;
你可以像在C语言中一样来完成它。这里有一个简单的例子:
int myGlobal = 3;
extern(我的变量)
,对吗? - futurevilla216[GlobalResource shareInstance].bgrMenuFileName;
我想要:
R.bgrMenuFileName
像这样做一些事情! 在GlobalResource.h文件中:
@class GlobalResource;
extern GlobalResource * R;
@interface GlobalResource: NSObject
+ (void) loadGlobalResources;
@end
// you must redeclare R
GlobalResource *R;
// then init it
@implementation GlobalResouce
+ (void) loadGlobalResources
{
R = [[GlobalResource alloc] init];
}
- (id)init
{
self = [super init];
if (self) {
// your init
}
return self;
}
@end
然后,在您的应用程序加载时调用loadGlobalResources:
[GlobalResource loadGlobalResources];
并将其导入到Prefix.pch中,以便在项目中随时使用R。
#import "GlobalResource.h"
使用它:
CCSprite *s = [CCSprite spriteWithFile:R.bgrMenuFileName];
//...