George
George

Reputation: 3629

What does @() mean in 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 */

Upvotes: 15

Views: 8587

Answers (4)

ilya n.
ilya n.

Reputation: 18826

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

Upvotes: 14

Prince Agrawal
Prince Agrawal

Reputation: 3607

It's Shorthand writing

In Objective-C, any character, numeric or boolean literal prefixed with the '@' character will evaluate to a pointer to an NSNumber object (In this case), initialized with that value. C’s type suffixes may be used to control the size of numeric literals.

'@' is used a lot in the objective-C world. It is mostly used to avoid taking english words and making them reserved (for example, you can't have a variable called float in C/Objective-C because this is a reserved word).

Use this link To have detailed knowledge of '@' symbol.

In Modern Objective C, '@' symbol is used extensively.

What You can do with it:

  • calculate an expression: @(<Expression>)
  • wrap any value like int,bool,float,char in same way

Reasons to use:

  • Easy to write, Less code required
  • Less chances of mistakes. Compare [NSNumber numberWithInt:3] with @3.
  • Get rid of typecasting issues in simple cases.

Upvotes: 3

Nayan
Nayan

Reputation: 3014

NeXT and Apple Obj-C runtimes have long included a short-form way to create new strings, using the literal syntax @"a new string". Using this format saves the programmer from having to use the longer initWithString or similar methods when doing certain operations.

When using Apple LLVM compiler 4.0 or later, arrays, dictionaries, and numbers (NSArray, NSDictionary, NSNumber classes) can also be created using literal syntax instead of methods. Literal syntax uses the @ symbol combined with [], {}, (), to create the classes mentioned above, respectively.

So, basically it's not only for id or NSNumber object!

thanks to wiki.

Upvotes: 7

Toseef Khilji
Toseef Khilji

Reputation: 17429

It represent id Object

that you can use any expression in it or return any object.

Syntax : @(<#expression#>) it will return id object.

So in your case it will returning NSNumber object to setToValue method.

Upvotes: 0

Related Questions