@() 在 Objective-C 中是什么意思? [英] What does @() mean in Objective-C?
问题描述
例如,
CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
[rotate setToValue:@(M_PI)];
[rotate setDuration:0.1f];
[[aView layer] addAnimation:rotate forKey:@"myRotationAnimation"];
其中M_PI
定义为math.h
中的宏,
#define M_PI 3.14159265358979323846264338327950288 /* pi */
推荐答案
它是一个指向 NSNumber
对象的指针.它被称为装箱文字,因为心理图景是将表达式的原始值放入一个盒子",即一个对象中.
It's a pointer to an NSNumber
object. It's called a boxed literal, because the mental picture is of putting a primitive value of expression inside into a "box", that is, an object.
如有疑问,请参阅官方文档.请注意,指针可以指向真实的"NSNumber
对象,也可以(理论上,不知道这在实践中是否可行)是标记指针(请参阅,例如,我的问题).
See official documentation if in doubt. Note that pointer can be to a "real" NSNumber
object or it can (theoretically, don't know whether this will work in practice) be a tagged pointer (see, e.g., my question).
请注意,您还可以执行诸如 @"string"
和 @5
之类的操作,它们将在编译时创建常量.但是你需要括号来使用不是文字的东西,例如@(2 + 3)
.括号形式可以用于任何表达式,即使是编译器在编译时无法计算的表达式(尽管如果可以,它只会将表达式结果放入代码中).
Note that you can also do things like @"string"
and @5
, which will create constants in compile time. But you need parentheses to use something which is not a literal, e.g. @(2 + 3)
. Parentheses form can be used for any expression, even those that compiler cannot compute at compile-time (although if it can, it will just put an expression result into code).
这篇关于@() 在 Objective-C 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!