Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving code from Xcode 3 to 4 produces error: Cannot declare variable inside @interface or @protocol

I am migrating my old iPhone apps from Xcode 3 to Xcode 4. I receive this error on code which used to build in Xcode 3 -- in fact I wrote it this way on purpose to hide implementation details from other modules. However, something seems to have changed in Objective-C. This code now receives the error

Cannot declare variable inside @interface or @protocol.

Remember, this is code at the top of the .m file not the .h

@interface VisualViewController ()
BOOL doReloadPhoto;

+ (void)buildVisualEffectInfo;

@property (nonatomic, retain) HandleCheckListSetting *checkListHandler;
@end
like image 408
halt00 Avatar asked Jan 16 '23 19:01

halt00


1 Answers

This may have compiled before, but I don't think it was doing what you think it's doing. It sounds like you want to create a private ivar in a class extension. The syntax you have is just a top-level variable, though, equivalent to:

BOOL doReloadPhoto;

@interface VisualViewController ()
// etc.
@end

You need to put it inside curly braces for it to be an ivar:

@interface VisualViewController () 
{
    BOOL doReloadPhoto;
}

(Conversely, I'm not sure that this -- ivar in extension -- was possible with Xcode 3's compiler, which is probably why you did it the way you did.) It sounds like the compiler is now (sensibly) pointing out that the code you have probably isn't doing what you expect, and forcing you to make your intention completely clear.

It has also recenty become possible to declare private ivars in the @implementation block, using the same syntax:

@implementation VisualViewController
{
    BOOL doReloadPhoto;
}
like image 131
jscs Avatar answered Jan 31 '23 08:01

jscs