IOS Weird compiler error: \"expected ']' b

2019-08-07 09:30发布

问题:

I have the weirdest error! In Xcode I have a singleton with the following defined (file is: MyGizmoClass.h):

NSString            *plistPath;

NSString            *dataDomain;
NSString            *pathToChatScript;
NSString            *pathToUpdates;

and

@property (nonatomic,retain) NSString *plistPath;

@property (nonatomic,retain) NSString *dataDomain;
@property (nonatomic,retain) NSString *pathToChatScript;
@property (nonatomic,retain) NSString *pathToUpdates;

I have a Constants.h file (which I #import early in my .pch file) that contains:

#define kUserPlistName  @"userPlist.plist"

#define kDataDomain @"http://www.jann.com/";
#define kPathToChatScript @"path/top/chatscript.cgi";
#define kPathToupdates @"pathtoupdates/";

Okay, so far, so good.

The order, in my .pch file is as such:

#import "Constants.h"
#import "MyGizmoClass.h"    

and then later in the Constants.h file I do this:

#import "FileFunctions.h"

Okay, fine setup. This should work, a singleton with 3 NSStrings. But when I try to do this in FileFunctions.h I get a weird error in the compiler:

FileFunctions.h

[myGizmoClass setDataDomain: kDataDomain];
[myGizmoClass setPathToChatScript: kPathToChatScript];
[myGizmoClass setPathToUpdates: kPathToupdates];

[myGizmoClass setPlistPath:[[myGizmoClass libraryDir]  stringByAppendingPathComponent:kUserPlistName]];

Compiler Results

Expected ']' before ;

What is going on? I get this 3 times ... one for setDataDomain, one for setPathToChatScript and one for setPathToUpdates. But no error on kUserPlistName. When I do this in FileFunctions.h then all is well:

[myGizmoClass setDataDomain: @"http://www.jann.com/"];
[myGizmoClass setPathToChatScript: @"path/top/chatscript.cgi"];
[myGizmoClass setPathToUpdates: @"pathtoupdates/"];

Compiler Results

All is well.

I cannot figure this out. Later (4 lines below this) I do the following:

[myGizmoClass setPlistPath:[[myGizmoClass libraryDir]  stringByAppendingPathComponent:kUserPlistName]];

And THAT works! Why would subbing kUserPlistName work, yet kDataDomain wouldn't? This is probably SO easy...but I cannot, for the life of me, see it!

ADDENDUM

I have found that EVERYWHERE I use kDataDomain, setPathToChatScript or setPathToUpdates in the code I get the same

Compiler Results

Expected ']' before ;

error! :(

Thanks!

回答1:

Don't include the semicolon in your #defines.

The compiler substitutes in exactly what you have there, so these would be equivalent:

[myGizmoClass setPathToUpdates: kPathToupdates];
[myGizmoClass setPathToUpdates: @"pathtoupdates/";]; // obvious syntax error