在 Objective-C 中,.m 文件中声明全局变量有什么具体规则吗?

发布于 2024-12-22 15:32:02 字数 595 浏览 4 评论 0原文

通过阅读这篇帖子,看起来应该考虑某些规则声明全局变量。

所以我尝试在 .m 文件中声明全局变量。然而,代码感觉似乎并不乐意为我做这件事。例如,假设我的 .m 文件中已经有 2 行:

@implementation ViewController
@合成abc,xyz;

现在我想添加“BOOL isChecked;”。

如果我在“@synthesize”下方(或在@implementation和@synthesize之间)输入此内容,代码感实际上建议我在输入“BOOL”时输入“bool”(小写)。如果我在“@implementation”上方输入“BOOL”,则会成功提示“BOOL”。

当然,全局变量是此类的一部分,这意味着它应该位于实现内部。我不知道为什么它不喜欢让我们这样做。

这让我感觉Objective-C不喜欢我们在@synthesize下面声明全局变量。但我的问题是为什么?我的感觉是,可能是有原因的,或者苹果在这里犯了一个bug。

By reading this post, It looks like certain rules should be considered when declaring global variables.

So I tried declaring global variables in the .m file. However, the code sense seems not happy to do this for me. For example, say I already have 2 lines in the .m file:

@implementation ViewController
@synthesize abc, xyz;

Now I want to add "BOOL isChecked;".

If I type this below "@synthesize" (or just between @implementation and @synthesize), the code sense actually suggests me to input "bool" (lower case) as I am typing "BOOL". If I type "BOOL" above "@implementation", it would suggest "BOOL" successfully.

Surely, the global variable is part of this class which means it should be inside the implementation. I am not sure why it doesn't like to let us do this.

This makes me feel that Objective-C doesn't like us to declare global variables below @synthesize. But my question is WHY? What I feel is that there may be a reason or Apple made a bug here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒夜笙歌凉 2024-12-29 15:32:02

当然,全局变量是此类的一部分,这意味着它应该位于实现内部。我不知道为什么它不喜欢让我们这样做。

全局变量不是类的一部分。当然,您可以将它们放在 @implementation 块中,但它们实际上不是类的一部分——它们是全局的——所以它们并不真正属于那里。

Objective-C 不像 Java 或其他语言那样有类变量。您可以使用全局变量和访问这些变量的类方法来伪造它们,但最终,它们是全局的,而不是特定于某个类的。

Surely, the global variable is part of this class which means it should be inside the implementation. I am not sure why it doesn't like to let us do this.

Global variables aren't part of a class. Sure, you can put them inside an @implementation block, but they're really not a part of the class -- they're global -- so they don't really belong there.

Objective-C doesn't have class variables like Java or other languages do. You can fake them with global variables and class methods that access those variables, but at the end of the day, they're global, not specific to a class.

怕倦 2024-12-29 15:32:02

IDE 的代码感知帮助所建议的内容并不绝对表明 Objective C 语言“喜欢”什么。将全局变量放在类实现的外部或内部会从 Xcode Objective C 编译器生成等效的编译结果,并且不会出现警告。

然而,将这些声明放在类实现之外可能被认为是更好的代码风格,因为所有全局变量实际上都具有全局作用域,而不是类作用域。

此外,您可能希望为全局变量分配一个初始值,而不是在代码中对其可能是什么进行任何假设。

What the IDE's code sense help suggests is not an absolute indication of what the Objective C language "likes". Putting a global variable either outside or inside a class implementation generates equivalent compiled results from the Xcode Objective C compiler, and with no warnings.

However it might be considered better code style to put these declarations outside of the class implementation, as all global variables actually have global scope, not class scope.

In addition, you might want to assign an initial value to your global variables, instead of making any assumptions in your code of what that might be.

花开半夏魅人心 2024-12-29 15:32:02

Objective-C 没有类变量。您声明一个 C 全局变量。我是这样做的:

声明一个静态 C 变量。对于您的情况:

static BOOL isChecked = NO;

请注意,我已对其进行了初始化。请注意,它被声明为静态,这限制了它对其声明所在的 .m 文件的可见性。

如果您想要一个全局对象(例如数组),则需要对其进行初始化:

static NSArray *myArray;

@implementation MyClass

+ (void)initialize {
    if (self == [MyClass class]) {
        myArray = [NSArray arrayWithObjects: ... ];
    }
}

@end

注意 if (self == [ MyClass 类]) 检查。 +initialize 将被调用一次或多次。第一次使用MyClass时(例如调用+alloc)。当第一次使用 MyClass 的任何子类时,可能会再次出现。因此,需要检查您是否实际上正在初始化 MyClass 或其子类。

最后,如果您想在声明该变量的代码之外访问该变量,请创建一个访问器:

+ (BOOL)isChecked {
    return isChecked;
}

不要通过删除 static 修饰符来公开全局 C 变量。它使调试、重构或重新实现变得更加困难(如果 isChecked 突然依赖于其他状态怎么办,如果它直接在其他代码中到处使用,您如何更改它?)

Objective-C does not have class variables. You declare a C global variable. This is how I do it:

Declare a static C variable. In your case:

static BOOL isChecked = NO;

Notice that I have initialized it. Notice that it is declared static, which restricts its visibility to the .m file it is declared in.

If you want a global object such as an array, you will need to initialize it:

static NSArray *myArray;

@implementation MyClass

+ (void)initialize {
    if (self == [MyClass class]) {
        myArray = [NSArray arrayWithObjects: ... ];
    }
}

@end

Notice the if (self == [MyClass class]) check. +initialize will be called one or more times. Once when MyClass is used for the first time (e.g. call to +alloc). Possibly again when any subclass of MyClass is used for the first time. Hence the check to see if you are actually initializing MyClass, or a subclass of it.

And finally, if you want to access this variable outside of the code that it is declared in, create an accessor:

+ (BOOL)isChecked {
    return isChecked;
}

Don't expose the global C variable by removing the static modifier. It makes it harder to debug, refactor or re-implement (what if isChecked suddenly depends on other state, how do you change this if it is directly used all over the place in other code?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文