我在哪里必须声明静态变量? [英] Where do I have to declare static variables?

查看:121
本文介绍了我在哪里必须声明静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即我想把这个在我的代码:

i.e. I want to bring this in my code:

static BOOL MyConstantBool = YES;

是否必须在@implementation之前或之后?有规则在哪里放置吗?它也可以进入头文件?

Must it be before or after @implementation? Are there rules where to place that? Can it also go into the header file?

推荐答案

如果你想定义一个全局变量,它(在@implementation的内部或外部)。在这个上下文中, static 表示该变量只能在此编译单元(.m文件)中可见。

If you want to define a global variable, it does not matter where you put it (inside or outside of @implementation). In this context, static means that the variable is only visible from within this compilation unit (.m file).

还有静态变量,它们在函数中定义。它们的工作方式类似于全局变量,但仅在函数的作用域内可见。

There are also static variables, which are defined in functions. They work like global variables, but are visible only from within the function's scope.

这篇关于我在哪里必须声明静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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