im trying to run my app from the menu bar and i added the following to the AppDelegate.h and i keep getting the error "cannot declare variable inside @protocol or @interface"
我试图从菜单栏运行我的应用程序,我将以下内容添加到AppDelegate.h,我不断收到错误“无法在@protocol或@interface内声明变量”
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSMenu *statusMenu;
NSStatusItem * statusItem;
}
can anyone help please? many thanks andrew
有人可以帮忙吗?非常感谢安德鲁
2 个解决方案
#1
1
I'm betting that's not your actual code. The error you got indicates that you forgot the braces around your instance variables.
我打赌这不是你的实际代码。您收到的错误表明您忘记了实例变量周围的大括号。
Incidentally, these days it's more common just to declare properties in the header, and if you need to declare actual instance variables, you do it in braces at the beginning of the @implementation
block. That way, properties are part of your public interface and instance variables are (properly) a private part of the implementation.
顺便说一句,现在更常见的是在头文件中声明属性,如果需要声明实际的实例变量,可以在@implementation块开头的大括号中进行。这样,属性就是公共接口的一部分,而实例变量(正确地)是实现的私有部分。
#2
0
You should really define them as a property instead
您应该将它们定义为属性
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (nonatomic, weak) IBOutlet NSMenu *statusMenu;
@property (nonatomic, strong) NSStatusItem * statusItem;
...
@end
'Why?', you may ask. Multiple reasons, but it's mainly because they do work, and because ARC now knows how to manage those variables.
“为什么?”,你可能会问。有多种原因,但这主要是因为它们确实有效,而且因为ARC现在知道如何管理这些变量。
#1
1
I'm betting that's not your actual code. The error you got indicates that you forgot the braces around your instance variables.
我打赌这不是你的实际代码。您收到的错误表明您忘记了实例变量周围的大括号。
Incidentally, these days it's more common just to declare properties in the header, and if you need to declare actual instance variables, you do it in braces at the beginning of the @implementation
block. That way, properties are part of your public interface and instance variables are (properly) a private part of the implementation.
顺便说一句,现在更常见的是在头文件中声明属性,如果需要声明实际的实例变量,可以在@implementation块开头的大括号中进行。这样,属性就是公共接口的一部分,而实例变量(正确地)是实现的私有部分。
#2
0
You should really define them as a property instead
您应该将它们定义为属性
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (nonatomic, weak) IBOutlet NSMenu *statusMenu;
@property (nonatomic, strong) NSStatusItem * statusItem;
...
@end
'Why?', you may ask. Multiple reasons, but it's mainly because they do work, and because ARC now knows how to manage those variables.
“为什么?”,你可能会问。有多种原因,但这主要是因为它们确实有效,而且因为ARC现在知道如何管理这些变量。