@interface Connections()
{
static Connections *this;
}
@end
上面.m文件中的一段代码抛出编译错误
Type name does not allow storage class to be specified
同时
static
关键字被删除它运作良好 - 这很明显。 目的:我想要静态和私有(private)的“Connections”实例。
为什么会这样,求助
最佳答案
您不能在 Objective-C 类中声明类级变量;相反,您需要将它们“隐藏”在实现文件中,通常为它们提供 static
作用域,这样它们就无法从外部访问。
连接.m:
#import "Connections.h"
static Connections *_sharedInstance = nil;
@implementation Connections
...
@end
如果这是一个单例,您通常会定义一个类级访问器以在首次使用时创建单例:
+ (Connections *)sharedInstance
{
if (_sharedInstance == nil)
{
_sharedInstance = [[Connections alloc] init];
}
return _sharedInstance;
}
(您需要在 .h 文件中添加声明):
+ (Connections *)sharedInstance;
关于ios - Objective-C - 声明错误 - 请解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14960249/