全局整数变量目标c [英] Global int variable objective c

查看:47
本文介绍了全局整数变量目标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屋!

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