简体   繁体   中英

Error linking constants to pch in Objective-C XCode

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM