I'm trying to link up some constants to my iOS app in XCode as per the answer here. I created a Constants.h header file like this:
// Constants.h
// myApp
extern NSString * const tumblrConsumerKey;
extern NSString * const tumblrConsumerSecret;
开发者_Python百科and a Constants.m implementation file like this:
// Constants.m
// myApp
#import "Constants.h"
NSString * const tumblrConsumerKey = @"keyiskey";
NSString * const tumblrConsumerSecret = @"secret";
I then added this to the top of my myApp-Prefix.pch precompiled header:
// Prefix header for all source files of the
// 'myApp' target in the 'myApp' project
//
#import <Availability.h>
#import "Constants.h"
Now I'm getting an error in the Constants.h file at the lines that declare extern NSString * const
etc:
Expected '=', ',', ';', 'asm' or '__attribute__' before
'*' token in /Users/me/Documents/iPhone Programs/myApp/myApp/Constants.h
It looks like my Constants.m file has been added to the target for myApp. What am I doing wrong?
It looks like you are being a bit eager with the placement of your include. It should be after the other frameworks
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Constants.h"
#endif
Also it's good practice to start constants with capital letters, normally 2-3 letters abbreviation of your name/company. It makes it easier to see you are dealing with a constant and not just a normal variable.
References:
Apple's recomendations on prefixing names
Apple's recommendations for declaring constants
精彩评论