目标C - 声明错误 - 请解释 [英] Objective C - Declaration Error - please explain

查看:265
本文介绍了目标C - 声明错误 - 请解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@interface Connections()
{
   static Connections *this;
}
@end

.m文件中的上述代码丢弃编译器错误

The above piece of code in .m file throwing compiler error


类型名称不允许指定存储类

Type name does not allow storage class to be specified

同时


static

static

关键字被删除它运作良好 - 这是显而易见的。
目的:我想要连接实例静态和私有。

key word is removed it works well - which so obvious. Purpose : I want "Connections" instance static and private.

为什么会出现这种情况,请帮忙。

Why is this behavior, please help.

推荐答案

您不能在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;

这篇关于目标C - 声明错误 - 请解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆