1、判断文件是否存在,再导入使用,避免文件不存在的情况;
- #if __has_include(<unistd.h>)
- # include <unistd.h>
- #elif __has_include(<io.h>)
- # include <io.h>
- #else
- # error "Insane system"
- #endif
- #if __has_include(<YYModel/YYModel.h>)
- FOUNDATION_EXPORT double YYModelVersionNumber;
- FOUNDATION_EXPORT const unsigned char YYModelVersionString[];
- #import <YYModel/NSObject+YYModel.h>
- #import <YYModel/YYClassInfo.h>
- #else
- #import "NSObject+YYModel.h"
- #import "YYClassInfo.h"
- #endif
2、FOUNDATION_EXPORT与#define 哪个来定义常量更好?
首先看看 苹果的定义
NSObjCRuntime.h中的写法:
- #if defined(__cplusplus)
- #define FOUNDATION_EXTERN extern "C"
- #else
- #define FOUNDATION_EXTERN extern
- #endif
-
- #if TARGET_OS_WIN32
-
- #if defined(NSBUILDINGFOUNDATION)
- #define FOUNDATION_EXPORT FOUNDATION_EXTERN __declspec(dllexport)
- #else
- #define FOUNDATION_EXPORT FOUNDATION_EXTERN __declspec(dllimport)
- #endif
-
- #define FOUNDATION_IMPORT FOUNDATION_EXTERN __declspec(dllimport)
-
- #else
- #define FOUNDATION_EXPORT FOUNDATION_EXTERN
- #define FOUNDATION_IMPORT FOUNDATION_EXTERN
- #endif
接下来继续,一般iOS我们定义常量的方法有两种,来看下面例子
我的.h文件
- FOUNDATION_EXPORT NSString * const kMyConstantString1;
- FOUNDATION_EXTERN NSString * const kMyConstantString2;
.m文件是这样定义的
- NSString * const kMyConstantString1 = @"123";
- NSString * const kMyConstantString2 = @"456";
- NSString *strTmp = @"456";
- if(<span style="font-family: Arial, Helvetica, sans-serif;">strTmp</span><span style="font-family: Arial, Helvetica, sans-serif;"> == kMyConstantString1) {</span>
-
-
- }
- NSLog(@"strTmp:%p, kMyConstantString2:%p", strTmp, kMyConstantString2);
打印结果:
- strTmp:0x103717248, kMyConstantString2:0x103717248
可见strTmp与
kMyConstantString2 指向同一内存地址。