本文介绍了如何实现全局静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个关于objective-c的新手问题:
我想在整个代码中创建一个可以被任何对象访问的静态类。这个类将充当几个对象的容器。
任何人都可以提供一个简短的代码示例,如何声明静态全局变量和方法?
This is kind of newbie question on objective-c:I would like to create a static class available throughout my entire code to be accessed by any object. This class will act as a container of several objects.Can anybody provide a short example of code as how to declare static global variables and methods ?
推荐答案
类我有这样的
.h文件看起来像这样
The .h file looks like this
@interface Globals : NSObject
{
}
+ (Globals *)instance;
@end
且.m文件如下
@implementation Globals
- (id)init
{
self = [super init];
if (self)
{
}
return self;
}
+ (Globals *)instance
{
static Globals *instance = nil;
@synchronized(self)
{
if (instance == nil)
{
instance = [[Globals alloc] init];
}
}
return instance;
}
- (void)dealloc
{
[super dealloc];
}
@end
Globals类的基本示例
of course this is a very basic example of a Globals class
这篇关于如何实现全局静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!