Boxing

Xcode 4.4 supports only literal scalar constants after the @. If you want to interpret a value and then convert it to a number object, you have to use the traditional method call:

NSNumber *two = [NSNumber numberWithInt:(1+1)];

Xcode 4.5 introduced boxed expression support, avoiding this awkward approach. Boxed expressions are values that are interpreted and then converted to number objects. A boxed expression is enclosed in parentheses, and it tells the compiler to evaluate and then convert to an object. For example:

NSNumber *two = @(1+1);

and

int foo = ...; // some value NSNumber *another = @(foo);

Boxed expressions are not limited to numbers. They work for strings as well. ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.