objective-c @符号在objective-c中代表什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the @ symbol represent in objective-c?
提问by Benedict Cohen
I'm learning objective-c and keep bumping into the @ symbol. It is used in different scenarios, for example at the start of a string or to synthesise accessor methods.
我正在学习objective-c并不断碰到@符号。它用于不同的场景,例如在字符串的开头或合成访问器方法。
What's does the @ symbol mean in objective-c?
Objective-c 中的@ 符号是什么意思?
回答by Chris Hanson
The @character isn't used in C or C++ identifiers, so it's used to introduce Objective-C language keywords in a way that won't conflict with the other languages' keywords. This enables the "Objective" part of the language to freely intermix with the C or C++ part.
@C 或 C++ 标识符中不使用该字符,因此它用于以不会与其他语言的关键字冲突的方式引入 Objective-C 语言关键字。这使得语言的“目标”部分能够与 C 或 C++ 部分自由混合。
Thus with very few exceptions, any time you see @in some Objective-C code, you're looking at Objective-C constructs rather than C or C++ constructs.
因此,除了极少数例外,任何时候您@在某些 Objective-C 代码中看到的都是 Objective-C 构造而不是 C 或 C++ 构造。
The major exceptions are id, Class, nil, and Nil, which are generally treated as language keywords even though they may also have a typedefor #definebehind them. For example, the compiler actually does treat idspecially in terms of the pointer type conversion rules it applies to declarations, as well as to the decision of whether to generate GC write barriers.
主要的例外是id、Class、nil和Nil,它们通常被视为语言关键字,即使它们后面也可能有 atypedef或#define。例如,编译器实际上id在其应用于声明的指针类型转换规则以及是否生成 GC 写屏障的决定方面确实进行了特殊处理。
Other exceptions are in, out, inout, oneway, byref, and bycopy; these are used as storage class annotations on method parameter and return types to make Distributed Objects more efficient. (They become part of the method signature available from the runtime, which DO can look at to determine how to best serialize a transaction.) There are also the attributes within @propertydeclarations, copy, retain, assign, readonly, readwrite, nonatomic, getter, and setter; those are only valid within the attribute section of a @propertydeclaration.
其他的例外是in,out,inout,oneway,byref,和bycopy; 这些用作方法参数和返回类型的存储类注释,以使分布式对象更高效。(他们变得可从运行时,它DO可以看一下,以确定如何最好地序列化交易的方法签名的一部分。)还有中的属性@property声明copy,retain,assign,readonly,readwrite,nonatomic,getter,和setter; 这些仅在@property声明的属性部分内有效。
回答by thelsdj
From Objective-C Tutorial: The @ Symbol, the reason it is on the front of various keywords:
来自Objective-C 教程:@ Symbol,它位于各种关键字前面的原因:
Using @ should make it easier to bolt an Objective-C compiler on to an existing C compiler. Because the @ isn't valid in any context in C except a string literal, the tokenizer (an early and simple step in the compiler) could be modified to simply look for the @ character outside of a string constant (the tokenizer understands string literals, so it is in a position to distinguish this). When @ is encountered the tokenizer would put the rest of the compiler in "Objective-C mode." (The Objective-C parser would be responsible for returning the compiler back to regular C mode when it detects the end of the Objective-C code).
使用 @ 应该可以更容易地将 Objective-C 编译器连接到现有的 C 编译器。因为@ 在 C 中除了字符串文字之外的任何上下文中都无效,所以可以修改分词器(编译器中早期的简单步骤)以简单地查找字符串常量之外的 @ 字符(分词器理解字符串文字) ,所以它可以区分这一点)。当遇到@ 时,分词器会将编译器的其余部分置于“Objective-C 模式”。(当检测到 Objective-C 代码结束时,Objective-C 解析器将负责将编译器返回到常规 C 模式)。
Also when seen in front of a string literal, it makes an NSString rather than a 'char *' in C.
同样,当在字符串文字前面看到时,它会在 C 中生成一个 NSString 而不是 'char *'。
回答by Van Nguyen
From Macrumors: Objective-C Tutorial, when in front of string literal:
来自Macrumors:Objective-C 教程,当在字符串文字前面时:
There are also @"" NSString literals. It is essentially shorthand for NSString's +stringWithUTF8String method.
还有@"" NSString 文字。它本质上是 NSString 的 +stringWithUTF8String 方法的简写。
The @ also adds unicode support to C strings.
@ 还为 C 字符串添加了 unicode 支持。
回答by pabloelustondo
From the manual:
从手册:
Objective-C frameworks typically do not use C-style strings. Instead, they pass strings around as NSString objects.
The NSString class provides an object wrapper for strings that has all of the advantages you would expect, including built-in memory management for storing arbitrary-length strings, support for Unicode, printf-style formatting utilities, and more. Because such strings are used commonly though, Objective-C provides a shorthand notation for creating NSString objects from constant values. To use this shorthand, all you have to do is precede a normal, double-quoted string with the @ symbol, as shown in the following examples:
NSString *myString = @"My String\n"; NSString *anotherString = [NSString stringWithFormat:@"%d %@", 1, @"String"];
Objective-C 框架通常不使用 C 风格的字符串。相反,它们将字符串作为 NSString 对象传递。
NSString 类为字符串提供了一个对象包装器,它具有您所期望的所有优点,包括用于存储任意长度字符串的内置内存管理、对 Unicode 的支持、printf 样式的格式化实用程序等等。因为这样的字符串被普遍使用,Objective-C 提供了一种用于从常量值创建 NSString 对象的速记符号。要使用这种速记,您所要做的就是在普通的双引号字符串前加上 @ 符号,如以下示例所示:
NSString *myString = @"My String\n"; NSString *anotherString = [NSString stringWithFormat:@"%d %@", 1, @"String"];

