在Objective-C XCode中将常量链接到pch时出错 [英] Error linking constants to pch in Objective-C XCode
问题描述
我正在尝试按照此处的答案在XCode中将一些常量链接到我的iOS应用.我创建了一个Constants.h头文件,如下所示:
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;
和类似这样的Constants.m实现文件:
and a Constants.m implementation file like this:
// Constants.m
// myApp
#import "Constants.h"
NSString * const tumblrConsumerKey = @"keyiskey";
NSString * const tumblrConsumerSecret = @"secret";
然后将其添加到myApp-Prefix.pch预编译头文件的顶部:
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"
现在,我在Constants.h文件中声明 extern NSString * const
等的行出现错误:
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
似乎我的Constants.m文件已添加到myApp的目标中.我在做什么错了?
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
同样好的做法是,以大写字母开头的常量,通常是您的姓名/公司的2-3个字母的缩写.这样一来,您就可以更轻松地看到您正在处理的是常量,而不仅仅是普通变量.
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.
参考文献:
Apple对 Apple关于声明
Apple's recommendations for declaring constants
这篇关于在Objective-C XCode中将常量链接到pch时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!