我想根据用户使用的是iPad还是iPhone来定义"常量"全局变量。现在,我正在尝试将这个文件globals.h包含在我的constants.pch文件中:
// globals.h
// BJ
#ifndef BJ_globals_h
#define BJ_globals_h
float LARGEST_FONT_SIZE = 30.0f;
float LARGE_FONT_SIZE = 20.0f;
float SMALL_FONT_SIZE = 16.0f;
float SMALLEST_FONT_SIZE = 12.0f;
float FONT_SIZE = 18.0f;
float CELL_CONTENT_WIDTH = 320.0f;
float CELL_MIN_HEIGHT = 50.0f;
float CELL_CONTENT_MARGIN = 10.0f;
float MIN_CELL_HEIGHT = .2f;
float SCROLL_VIEW_OFFSET = 0.1f;
float TABLE_VIEW_HEIGHT = 0.45f;
#endif
But when I include this in constants.pch with this call at the top of that file:
// constants.pch
#import "globals.h"
#ifndef BJ_constants_pch
#define BJ_constants_pch
/*
#define LARGEST_FONT_SIZE 30.0f
#define LARGE_FONT_SIZE 20.0f
#define SMALL_FONT_SIZE 16.0f
#define SMALLEST_FONT_SIZE 12.0f
#define FONT_SIZE 18.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_MIN_HEIGHT 50.0f
#define CELL_CONTENT_MARGIN 10.0f
#define MIN_CELL_HEIGHT .2f
#define SCROLL_VIEW_OFFSET 0.1f
#define TABLE_VIEW_HEIGHT 0.45f
*/
...
#endif
我实际上收到一个Mach-O链接器错误:
linker command failed with exit code 1 (use -v to see invocation)
如果我尝试在其他文件中包含 globals.h,例如在自定义类中,我不会收到此错误,但我不想在每个文件中单独包含它。我必须做一些不同的事情才能将此文件包含在 constants.pch 中吗?有没有另一种方法可以在应用程序中轻松、有条件地定义全局变量?我想根据用户使用的是iPhone还是iPad来设置字体和单元格大小。
感谢您的任何建议。
Ps 的最终目标是有条件地为整个应用程序的字体大小设置全局变量。所以我需要一个可以在运行时使用条件语句处理的文件。
您不能以这种方式包含常量。不能将头文件包含在 pch 中,其中包含如下常量:
float CELL_CONTENT_MARGIN = 10.0f;
您需要使用 #define
或extern
来定义它
喜欢:
#define CELL_CONTENT_MARGIN 10.0f
或
extern const float CELL_CONTENT_MARGIN;
我将我的常量文件包含在 #import <UIKit/UIKit.h>
和 #import <Foundation/Foundation.h>
之后的 .pch 中。
你和我的区别在于我的是这样的:
KRConstants.h:
#import <Foundation/Foundation.h>
FOUNDATION_EXPORT const int ddLogLevel;
// ...
KRConstants.m
const int ddLogLevel = LOG_LEVEL_VERBOSE;
// ...