全局整数变量目标c [英] Global int variable objective c
问题描述
我想在一个类中声明一个静态int变量,并在其他所有类中对其进行访问.最好的方法是什么?
I want to declare a static int variable in one class and have access to it in every other class. What is the best way to do this?
推荐答案
Objective C中没有静态类变量.您可以在类的实现文件中将其创建为文件范围的静态变量,并提供静态setter和getter类中的方法.
There are no static class variables in Objective C. You can create it as a file-scope static variable in the class' implementation file and provide static setter and getter methods in the class.
或者您也可以将其设置为老式的全局变量,并在.h文件中使用extern
声明.前一种方法更灵活-您可以在setter方法中添加额外的检查,例如,后者的类型较少,并且避免了方法调用的开销.
Or you can make it an old-school global, with an extern
declaration in the .h file. The former approach is more flexible - you can add extra checks in the setter method, for example, the latter is less typing, and avoids the method call overhead.
这篇关于全局整数变量目标c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!