OGeek|极客世界-中国程序员成长平台

标题: ios - 在 .h 文件中声明变量 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:38
标题: ios - 在 .h 文件中声明变量

只是想知道在 .h 文件中声明大量变量是否是一种良好的编程习惯。

我正在编写我的第一个应用程序,我通过它学习 xcode 和 obj-c。这个 ios 应用程序只有一个 xib、一个 .m 和一个 .h 文件。我发现我自己很多时候都有某个变量,我需要在 .m 文件中的不同方法/位置使用它,而我最终只是在 .h 文件中声明它,这似乎让我将变量设为全局变量我不认为拥有很多这些是一个好主意。

在 .h 文件中声明大量变量是否安全/可以,或者我应该以其他方式处理它吗?

谢谢



Best Answer-推荐答案


Is this safe/ok to have a lot of variables declared in .h file or should i approach it in some other way?

在 .h 中包含很多变量是绝对可以的!它只是稍微增加了编译时间,并将二进制文件的大小增加了任意数量。如果您担心,只需将您的实现分成几个类别即可。

I find my self a lot of times where i have a certain variable that i need to use in different methods/places in the .m file and i just end up declaring it in the .h file which seems like im making the variable global which i dont think is a good idea to have a lot of those.

在一种方法之外访问的变量应始终声明为 iVar,如果它们需要强引用或需要由外部类访问,则应声明为属性。全局变量是不同的,你不必担心。

关于ios - 在 .h 文件中声明变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9901348/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4