Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个 constants.h 文件,它声明了我在整个应用程序中使用的一堆字符串。我只有字符串,没有别的。我应该使用#define 还是静态 NSString const?#define 有效,但我听说它不是最佳实践。
这#define是一个预处理器宏。这意味着它基本上会遍历您的代码并用您定义的内容替换您的宏。
#define
如果使用 const,它将是指向内存中字符串的指针。这比在任何地方/何时使用相同的字符串分配更有效。
为此,您需要 .h 和 .m 文件。您的 .h 文件将类似于:
extern NSString * const YOUR_STRING;
还有你的 .m 文件:
NSString * const YOUR_STRING = @"your string";