更新时间:2022-12-24 14:31:35
您不能在Objective-C类中声明类级变量;相反,你需要在实现文件中隐藏它们,通常给它们 static
-scope,这样它们就无法从外部访问。
You cannot declare class-level variables in Objective-C classes; instead you need to "hide" them in the implementation file, often giving them static
-scope so they cannot be accessed externally.
Connections.m:
Connections.m:
#import "Connections.h"
static Connections *_sharedInstance = nil;
@implementation Connections
...
@end
如果这是一个单例,你通常会定义一个类级访问器来在第一次使用时创建单例:
And if this is a singleton, you typically define a class-level accessor to create the singleton upon first use:
+ (Connections *)sharedInstance
{
if (_sharedInstance == nil)
{
_sharedInstance = [[Connections alloc] init];
}
return _sharedInstance;
}
(你需要在.h文件中添加声明) :
(and you'll need to add the declaration in the .h file):
+ (Connections *)sharedInstance;