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

4
  • Do you have #import <UIKit/UIKit.h> before this code? Do you have @end after this code? Which line is the error on? Commented Jun 21, 2013 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: robsprogramknowledge.blogspot.com/2011/08/… Commented Jun 21, 2013 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. Commented Jun 21, 2013 at 16:49
  • OP, you might also want to check out this great SO post detailing where to declare variables: stackoverflow.com/questions/12632285/… Commented Jun 21, 2013 at 16:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.