为什么 .pch 文件在 swift 中不可用? [英] Why .pch file not available in swift?

查看:42
本文介绍了为什么 .pch 文件在 swift 中不可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 swift 中,不提供 Prefix.pch 文件.在我的项目中,我想全局声明一些宏并在整个应用程序中使用.

In swift, Prefix.pch file is not provided. In my project, I want to declare some macros globally and use through out application.

谁能给我建议怎么做,在哪里申报?

Can anyone give me suggestion how to do it, Where to declare?

有没有其他文件在 swift 中被 Prefix.pch 文件替换?

Is there any other file replaced by Prefix.pch file in swift?

推荐答案

即使在 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.

这篇关于为什么 .pch 文件在 swift 中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆