常量说明 [英] Explanation of constants

查看:98
本文介绍了常量说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找常量,除了不能以编程方式更改之外,我真的不了解它们的不同之处。

I have been looking about constants and I don't really understand whats different about them other than they cant be changed programmatically.

extern NSString * const MyConstant;

在此行中, extern 的确切含义以及 const 到底是什么意思?

With this line what exactly does extern mean and what exactly does the const mean?

推荐答案

这里有两个问题:一个关于常量,另一个关于外部。

You're asked two questions here: one about constants and one about extern. The two aren't necessarily related.

首先, const :常量没有什么比您说的要多的了不能通过编程方式进行更改。但是,不同的东西可以保持不变,这取决于您如何声明它们。例如,在您的示例中:

First, const: there isn't much more to constants than, as you said, they can't be changed programmatically. Different things can be constant though, depending on how you declare them. For instance, in your example:

NSString * const MyConstant = @"foo";

您已声明了指向非恒定NSString对象的恒定指针; const 关键字在星号的右边,因此它指向指针。因此,这是:

you have declared a constant pointer to a non-constant NSString object; the const keyword is to the right of the star, so it refers to the pointer. Thus, this:

MyConstant = @"bar";

会导致编译错误,因为它试图重新分配 MyConstant 指向不同的NSString。

would result in a compile error, since it's attempting to reassign MyConstant to point to a different NSString.

如果 const 关键字位于左侧,它将引用指针所引用的对象(在本例中为基础的NSString结构)。这可能不是您大多数时候在目标C中想要的。请注意, const 关键字相对于类型标识符的位置并不重要,因此:

If the const keyword were to the left of the star it would refer to the object the pointer references (in this case, the underlying NSString struct). This probably isn't what you want most of the time in Objective C. Note that the position of the const keyword relative to the type identifier doesn't matter, so this:

const NSString *MyConstant = @"foo";

这:

NSString const *MyConstant = @"foo";

表示同一件事。您还可以合法地声明指针和引用值const,以获取最大的稳定性:

mean the same thing. You can also legally declare both the pointer and the referenced value const, for maximum constness:

const NSString * const MyConstant = @"foo";

第二,外部外部仅允许您在一个编译单元中声明一个变量,并让编译器知道您已在单独的编译单元中定义了该变量。通常只将其用于全局值和常量。

Second, extern: extern simply allows you to declare a variable in one compilation unit, and let the compiler know that you've defined that variable in a separate compilation unit. You would generally use this only for global values and constants.

您可以将编译单元视为单个 .m 文件以及它包含的所有 .h 文件。在生成时,编译器将每个.m文件编译为一个单独的 .o 文件,然后链接程序将它们全部挂接到一个二进制文件中。通常,一个编译单元了解另一编译单元中声明的标识符(例如类名)的方式是通过导入头文件。但是,对于全局变量,它们通常不是类的公共接口的一部分,因此经常在 .m 文件中声明和定义它们。

You can think of a compilation unit as a single .m file, as well as all the .h files it includes. At build time the compiler compiles each .m file into a separate .o file, and then the linker hooks them all together into a single binary. Usually the way one compilation unit knows about identifiers (such as a class name) declared in another compilation unit is by importing a header file. But, in the case of globals, they are often not part of a class's public interface, so they're frequently declared and defined in a .m file.

如果编译单元A在 .m 文件中声明了一个全局变量:

If compilation unit A declares a global in a .m file:

#import "A.h"

NSString *someGlobalValue;

,编译单元B希望使用该全局变量:

and compilation unit B wants to use that global:

#import "B.h"
extern NSString *someGlobalValue;

@implementation B

- (void)someFunc {
    NSString *localValue = [self getSomeValue];
    if (localValue isEqualToString:someGlobalValue]) {
        ...
    }
}

单元B必须以某种方式告诉编译器使用单元A声明的变量。它不能导入 .m 文件,声明发生,因此它使用 extern 告诉编译器该变量在其他位置。

unit B has to somehow tell the compiler to use the variable declared by unit A. It can't import the .m file where the declaration occurs, so it uses extern to tell the compiler that the variable exists elsewhere.

请注意,如果单元A和单元B 两者在文件的顶层都有此行:

Note that if unit A and unit B both have this line at the top level of the file:

NSString *someGlobalValue;

然后,您有两个声明相同全局变量的编译单元,链接器将失败,并出现重复的符号错误。如果要让这样的变量在编译单元中仅存在 ,并且对于任何其他编译单元不可见(即使它们使用 extern ),则可以使用 static 关键字:

then you have two compilation units declaring the same global variable, and the linker will fail with a duplicate symbol error. If you want to have a variable like this that exists only inside a compilation unit, and is invisible to any other compilation units (even if they use extern), you can use the static keyword:

static NSString * const someFileLevelConstant = @"wibble";

这对于要在单个实现文件中使用的常量很有用,但不会

This can be useful for constants that you want to use within a single implementation file, but won't need elsewhere.

这篇关于常量说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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