Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot declare variable inside @interface or @protocol

Tags:

ios

iphone

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?

like image 797
cactusdev Avatar asked Oct 31 '11 19:10

cactusdev


4 Answers

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
like image 189
Simeon G Avatar answered Oct 20 '22 05:10

Simeon G


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

like image 28
fluffy Avatar answered Oct 20 '22 04:10

fluffy


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.

like image 3
Kevin Avatar answered Oct 20 '22 05:10

Kevin


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.

like image 1
hotpaw2 Avatar answered Oct 20 '22 05:10

hotpaw2