ios 不能在 @interface 或 @protocol 中声明变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7958939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:08:40  来源:igfitidea点击:

Cannot declare variable inside @interface or @protocol

iphoneios

提问by cactusdev

I have an iOS app built since the beginning with an error in it. Since the source was began constructed from the template, its appdelegate.h looks like:

我有一个从一开始就构建的 iOS 应用程序,其中有一个错误。由于源是从模板开始构建的,它的 appdelegate.h 看起来像:

@interface myAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    myViewController *viewController;
}

BOOL       myBool;     // intended to be globally accessible
NSString   *myString;  // intended to be globally accessible

@end

I refer to myBooland *myStringfrom many other .m source files, as to global variables.

关于全局变量,我从许多其他 .m 源文件中引用myBool*myString

Below XCode 3.2.6, I can not remember getting any issues at compile time.

在 XCode 3.2.6 以下,我不记得在编译时遇到任何问题。

At 3.2.6, warning appeared at compile pointing to these “global” variables in appdelegate.h, saying: “Cannot declare variable inside @interface or @protocol”. As there were no further problems with compilation or during app runtime, unfortunately I did not consider these warnings.

在 3.2.6 中,在 compile 出现警告,指向 appdelegate.h 中的这些“全局”变量,说:“无法在 @interface 或 @protocol 中声明变量”。由于编译或应用程序运行期间没有进一步的问题,不幸的是我没有考虑这些警告。

Now, using XCode 4.2, I am unable to compile this source, because the former warnings turned into build errors. They refer and point to each of those lines in the different .m files where there is a reference to the “global variables”.

现在,使用 XCode 4.2,我无法编译这个源代码,因为之前的警告变成了构建错误。它们引用并指向不同 .m 文件中的每一行,其中引用了“全局变量”。

Is there an easy way to correct this problem, considering that I still want to access these variables/references as global ones?

考虑到我仍然想将这些变量/引用作为全局变量/引用访问,是否有一种简单的方法可以纠正这个问题?

Additional question: while I am evaluating so far received answers (thanks for all of you), another question: any idea why no warning were given below XCode v3.2.6, and only warnings in 3.2.6 if this is a real error from my side? And why the code was still compiled and could be run without any problem?

附加问题:虽然我正在评估到目前为止收到的答案(感谢你们所有人),另一个问题:知道为什么在 XCode v3.2.6 下没有给出警告,如果这是我的真正错误,只有 3.2.6 中的警告边?为什么代码仍然被编译并且可以毫无问题地运行?

回答by Simeon G

They can't go there. You can put them inside the curly braces {} like this:

他们不能去那里。您可以将它们放在花括号 {} 中,如下所示:

@interface myAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    myViewController *viewController;
BOOL       myBool;     // intended to be globally accessible
NSString   *myString;  // intended to be globally accessible
}

@end

And that makes them global to the implementation class. But if you want them global to every class in your app then you should drop them in your App-Prefix.pch file:

这使得它们对于实现类是全局的。但是如果你想让它们对你的应用程序中的每个类都是全局的,那么你应该将它们放在你的 App-Prefix.pch 文件中:

//
// Prefix header for all source files of the ... project
//
#import <Availability.h>
BOOL       myBool;     // intended to be globally accessible
NSString   *myString;  // intended to be globally accessible
#ifndef __IPHONE_3_0

回答by fluffy

Are you trying to define them as public members on a class? Classes in Objective-C are rather different than in other languages you might be familiar with. Outside of the curly braces you can only define methods. If you want to make a publicly-accessible member, define them as properties:

您是否试图将它们定义为类的公共成员?Objective-C 中的类与您可能熟悉的其他语言中的类有很大不同。在花括号之外,您只能定义方法。如果要创建可公开访问的成员,请将它们定义为属性:

@interface myAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    myViewController *viewController;
    BOOL _myBool;
    NSString *_myString;
}

@property BOOL       myBool;     // intended to be globally accessible
@property NSString   *myString;  // intended to be globally accessible

@end

Then in your @implementation do something like:

然后在您的@implementation 中执行以下操作:

@implementation myAppDelegate
@synthesize myBool = _myBool;
@synthesize myString = _myString;

Then you can access them as myObject.myBooland so on.

然后你可以访问它们myObject.myBool等等。

If you are just trying to make them into static ("global") data for all instances of the class, then as other posters have said, you want to move the definition into your .mfile (and ideally declare them staticso they won't cause link issues).

如果您只是想将它们变成类的所有实例的静态(“全局”)数据,那么正如其他海报所说,您希望将定义移动到您的.m文件中(并理想地声明它们,static以便它们不会导致链接问题)。

回答by Kevin

The compiler is complaining about the variables being in the @interfaceblock, so move them out of it, either above the @interfaceor below @end. You'll actually probably want to change them to externs in the header and actually declare them in the .m file.

编译器抱怨变量在@interface块中,因此将它们移出块,无论是在 之上@interface还是之下@end。您实际上可能希望extern在标题中将它们更改为s 并在 .m 文件中实际声明它们。

回答by hotpaw2

C Global variables should be declared in .m implementation files, not in .h header files. An extern declaration can go in the .h header files, usually after the includes and outside the interface declarations.

C 全局变量应该在 .m 实现文件中声明,而不是在 .h 头文件中。extern 声明可以放在 .h 头文件中,通常在包含之后和接口声明之外。

It's also good practice to initialize global object pointers to nil, or else they might contain a garbage object reference.

将全局对象指针初始化为 nil 也是一种很好的做法,否则它们可能包含垃圾对象引用。