New Objective-c Literals in 4.4
Asked Answered
S

1

4

I can write @42, which creates an NSNumber with int value 42. Can I do this with a variable, like @someIntVar? Obviously I tried it and it doesn't work (which sucks because then I have to go through [NSNumber numberWithInt:someIntVar]). Is it possible with a slightly different syntax?

Simsar answered 25/8, 2012 at 7:41 Comment(0)
K
8

I strongly suggest you read the official clang documentation on the matter: http://clang.llvm.org/docs/ObjectiveCLiterals.html

But, to box a variable, or any expression, you can use parentheses:

 id num = @(someIntVar);
Kauffman answered 25/8, 2012 at 7:43 Comment(1)
Just to add to that, you can also do that with C string variables to create NSStrings (same @(...) syntax)Lund

© 2022 - 2024 — McMap. All rights reserved.