目标 C 中的全局变量 [英] Global Variables in Objective C

查看:72
本文介绍了目标 C 中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计数器,我用它来获取该计数器索引处的对象,我需要在另一个类中访问它.

Objective C 中是如何声明静态变量的?

解决方案

嗨 alJaree,
您在您的类的实现中声明了一个静态变量,并通过静态访问器启用对它的访问:<代码><预>some_class.h:@interface SomeClass {...}+ (int) 计数器;@结尾some_class.m:@implementation SomeClass静态整数计数器;+ (int)counter { 返回计数器;}@end

I have a counter which I use to get an object at that counters index and I need to access it in another class.

How are static variables declared in Objective C?

解决方案

Hi alJaree,
You declare a static variable in the implementation of Your class and enable access to it through static accessors:

some_class.h:
@interface SomeClass {...}
+ (int)counter;
@end

some_class.m: 
@implementation SomeClass
static int counter;
+ (int)counter { return counter; }
@end

这篇关于目标 C 中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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