无法在 @interface 或 @protocol 内声明变量

发布于 2024-12-13 00:11:10 字数 899 浏览 0 评论 0原文

我从一开始就构建了一个 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

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

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

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

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

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

附加问题:虽然我正在评估迄今为止收到的答案(谢谢大家),但另一个问题:知道为什么 XCode v3.2.6 以下没有发出警告,而在 3.2.6 中仅发出警告,如果这是我这边真正的错误吗?为什么代码仍然可以编译并且可以毫无问题地运行?

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:

@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 myBool and *myString from many other .m source files, as to global variables.

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

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.

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”.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赏烟花じ飞满天 2024-12-20 00:11:10

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

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

@end

这使得它们对于实现类是全局的。但如果您希望它们对应用程序中的每个类都是全局的,那么您应该将它们放在 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

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:

//
// 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
悲念泪 2024-12-20 00:11:10

您是否想将它们定义为类中的公共成员? 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

然后在您的 @implementation 中执行类似的操作:

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

然后您可以通过 myObject.myBool 等方式访问它们。

如果您只是想将它们变成类的所有实例的静态(“全局”)数据,那么正如其他发帖者所说,您希望将定义移动到您的 .m 文件中(并且理想情况下将它们声明为静态,这样它们就不会导致链接问题)。

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:

@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 myAppDelegate
@synthesize myBool = _myBool;
@synthesize myString = _myString;

Then you can access them as myObject.myBool and so on.

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 .m file (and ideally declare them static so they won't cause link issues).

罗罗贝儿 2024-12-20 00:11:10

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

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

初心未许 2024-12-20 00:11:10

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

将全局对象指针初始化为 nil 也是一个好习惯,否则它们可能包含垃圾对象引用。

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文