且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 .pch 文件在 swift 中不可用?

更新时间:2023-02-24 08:05:01

即使在 Obj-C 中,对常量和表达式使用宏也是你不应该做的事情.从你的评论中举例:

Even in Obj-C, using Macros for constants and expressions is something you shouldn't do. Taking examples from your comments:

#define NAVIGATIONBAR_COLOR @"5B79B1"

***作为UIColor上的分类方法,例如

It would be better as a category method on UIColor, for example

+ (UIColor *) navigationBarColor {
    return [UIColor colorWith...];
}

isPad 宏应该是一个普通函数

The isPad macro should be either a plain function

BOOL isIPad() {
    return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
}

或再次分类方法,例如UIDevice

or again a category method, e.g. on UIDevice

[UIDevice isIPad]

定义为

+ (BOOL)isIPad {
   return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
}

预编译的头文件从来没有打算将宏共享给您的所有代码.它们被用来在那里包含框架头文件以加快编译过程.随着去年模块的引入,现在可以创建自定义模块,您不再需要预编译的头文件.

The precompiled headers were never meant to share macros to all of your code. They were made to include framework headers there to speed up the compilation process. With the introduction of modules last year and now with the possibility to create custom modules, you don't need precompiled headers any more.

在 Swift 中情况是一样的——没有头文件和宏,所以也没有预编译的头文件.改用扩展、全局常量或单例.

In Swift the situation is the same - there are no headers and no macros so there is also no precompiled header. Use extensions, global constants or singletons instead.