我正在尝试 link up some constants to my iOS app in XCode as per the answer here .我创建了一个 Constants.h 头文件,如下所示:
// Constants.h
// myApp
extern NSString * const tumblrConsumerKey;
extern NSString * const tumblrConsumerSecret;
和一个像这样的 Constants.m 实现文件:
// Constants.m
// myApp
#import "Constants.h"
NSString * const tumblrConsumerKey = @"keyiskey";
NSString * const tumblrConsumerSecret = @"secret";
然后我将它添加到 myApp-Prefix.pch 预编译头文件的顶部:
// Prefix header for all source files of the
// 'myApp' target in the 'myApp' project
//
#import <Availability.h>
#import "Constants.h"
现在我在声明
extern NSString * const
的行中的 Constants.h 文件中遇到错误。 ETC:Expected '=', ',', ';', 'asm' or '__attribute__' before
'*' token in /Users/me/Documents/iPhone Programs/myApp/myApp/Constants.h
看起来我的 Constants.m 文件已添加到 myApp 的目标中。我究竟做错了什么?
最佳答案
看起来您对包含的位置有点急切。它应该在其他框架之后
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Constants.h"
#endif
此外,以大写字母开头的常量也是一个好习惯,通常是您的姓名/公司的 2-3 个字母缩写。它使您更容易看到您正在处理一个常量,而不仅仅是一个普通变量。
引用文献:
Apple 对 prefixing names 的建议
Apple 建议声明 constants
关于iphone - 在 Objective-C XCode 中将常量链接到 pch 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7439011/