1

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"

       @interface AppDelegate : NSObject <NSApplicationDelegate>
       {
       IBOutlet NSMenu *statusMenu;
       NSStatusItem * statusItem;
       }

can anyone help please? many thanks andrew

marko
  • 9,029
  • 4
  • 30
  • 46
  • Do you have `#import ` before this code? Do you have `@end` after this code? Which line is the error on? – Steve Waddicor Jun 21 '13 at 16:46
  • I have code that is identical to that in released apps, so it doesn't look like the issue is in this part of your code. Do you have any protocols you declare in your app? The problem might be there. On another note, you should seriously consider using properties for your variables. Robert Brown makes a good case for minimizing the use of ivars here: http://robsprogramknowledge.blogspot.com/2011/08/objective-c-property.html – JiuJitsuCoder Jun 21 '13 at 16:48
  • 1
    @SteveWaddicor Yeah you're right. Sorry, I wasn't paying attention to where I clicked! I deleted my answer and added a comment instead. – JiuJitsuCoder Jun 21 '13 at 16:49
  • OP, you might also want to check out this great SO post detailing where to declare variables: http://stackoverflow.com/questions/12632285/declaration-definition-of-variables-locations-in-objectivec – JiuJitsuCoder Jun 21 '13 at 16:51

2 Answers2

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.

Chuck
  • 234,037
  • 30
  • 302
  • 389
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.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105