Objective-C:在哪里定义宏可以在任何地方使用? [英] Objective-C: Where to define macros to be available everywhere?

查看:119
本文介绍了Objective-C:在哪里定义宏可以在任何地方使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用程序,它在许多视图控制器中使用相同的字符串,数字等。所以我认为在一个文件中定义这些常量并在所有视图控制器实现中使用它会好得多。优点是改变一个数字,我只需要做一次而不是在所有视图控制器中使用这个数字。

I have an iOS application which uses in many view controllers, the same strings, numbers, etc. So I think it would be much better to define these constants in one file and use it in all the view controller implementations. The advantage will be changing one number, I have to do it only once and not in all view controllers this number is used.

实际上,我有我的.h文件这样的宏:

Actually, I have in my .h file such macros:

#define aNumber 34.0

然后我可以在相应的.m文件中使用它。但是在整个应用程序中执行此操作的最佳方法是什么?创建一个只包含这些宏的普通.h文件,然后在视图控制器的.h文件中导入该文件?

and then I can use it in the corresponding .m file. But what's the best way to do this in the whole application? Creating a plain .h file which consists only of such macros and then importing this file in the .h files of the view controllers?

最诚挚的问候,蒂姆。

推荐答案

简单的方法是使用constant.h并在那里写所有宏。我们有.pch类并将constant.h导入到。 pch(预编译头文件)file.No需要将constant.h文件导入到每个viewcontroller。

The simple way is take constant.h and write all macro there.we have .pch class and import the constant.h into the .pch(pre compile header) file.No need to import constant.h file to each and every viewcontroller.

这篇关于Objective-C:在哪里定义宏可以在任何地方使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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