objective-c - 将代码从 Xcode 3 移动到 4 会产生错误 : Cannot declare variable inside @interface or @protocol

标签 objective-c xcode4 compiler-errors instance-variables class-extensions

我正在将我的旧 iPhone 应用程序从 Xcode 3 迁移到 Xcode 4。我在用于在 Xcode 3 中构建的代码中收到此错误——事实上,我是故意这样写的,以隐藏其他模块的实现细节。但是,Objective-C 中似乎发生了一些变化。此代码现在收到错误

Cannot declare variable inside @interface or @protocol.

请记住,这是 .m 文件顶部的代码,而不是 .h

@interface VisualViewController ()
BOOL doReloadPhoto;

+ (void)buildVisualEffectInfo;

@property (nonatomic, retain) HandleCheckListSetting *checkListHandler;
@end

最佳答案

这可能以前编译过,但我不认为它在做你认为它在做的事情。听起来您想在类扩展中创建一个私有(private) ivar。不过,您拥有的语法只是一个顶级变量,相当于:

BOOL doReloadPhoto;

@interface VisualViewController ()
// etc.
@end

你需要把它放在大括号内才能成为ivar:

@interface VisualViewController () 
{
    BOOL doReloadPhoto;
}

(相反,我不确定 Xcode 3 的编译器是否可以实现这一点——扩展名中的 ivar,这可能就是你按照你的方式做的原因。)听起来编译器现在(明智地)指出您拥有的代码可能没有按照您的预期进行,并迫使您完全清楚地说明您的意图。

最近也可以使用相同的语法在@implementation block 中声明私有(private)ivars:

@implementation VisualViewController
{
    BOOL doReloadPhoto;
}

关于objective-c - 将代码从 Xcode 3 移动到 4 会产生错误 : Cannot declare variable inside @interface or @protocol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10366866/

相关文章:

ios - 自动布局 - 按百分比定位

objective-c - 从 NSArray 获取字符串,使用 NSUserDefaults 保存

iOS 包含来自另一个 xcode 项目的 UIViewController

xcode - 安装程序显示 "Successfully Installed"后看不到 Xcode

ios - UITableView 一次仅勾选一行

c++ - 变量或字段 'name of var'声明为void

objective-c - 从框架呈现模态视图时如何检测何时按下 "Done"按钮

html - 在父窗口中打开 iFrame 中的链接(无法控制子站点)

assembly - NASM 错误 : parser instruction expected

c++ - 范围内变量的重新定义