Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does @() mean in Objective-C?

Tags:

objective-c

For example,

CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
[rotate setToValue:@(M_PI)];
[rotate setDuration:0.1f];            
[[aView layer] addAnimation:rotate forKey:@"myRotationAnimation"];

where M_PI is defined as a macro in math.h,

#define M_PI  3.14159265358979323846264338327950288   /* pi */
like image 309
George Avatar asked Jan 13 '14 04:01

George


People also ask

What does the symbol mean in Objective-C?

That symbol is used to declare block. For more information read here Blocks Programming Topics. Some more info: Block objects are a C-level syntactic and runtime feature.

Is Objective-C hard to learn?

Aside from its funny-looking syntax, Objective-C is an easier language for beginner developers to learn.

Is Objective-C and C the same?

Syntactically, Objective-C is an extension of C. So, some portion of Objective-C is exactly the same as C. Your experience of C would help learning such aspect of Objective-C. But the core part of Objective-C programming is made of Object Oriented class system, which you cannot find in C.


1 Answers

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.

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).

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).

like image 62
ilya n. Avatar answered Oct 11 '22 12:10

ilya n.