IOS Weird compiler error: "expected ']' before ;' using set-ter for NSString constant in singleton

走远了吗. 提交于 2019-12-11 07:48:58

问题


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


来源:https://stackoverflow.com/questions/7194313/ios-weird-compiler-error-expected-before-using-set-ter-for-nsstring-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!