Objective-C Auto-unboxing with LLVM 4
Asked Answered
P

2

6

I've been looking at autoboxing in Objective-C (here, for instance). Is there a new syntax for unboxing?

For instance, I want to do this but shorter:

NSArray *oneNumber = @[@1];
int one = ((NSNumber *)oneNumber[0]).intValue;

the second line's syntax is horrific. Is there any new language feature to deal with this?

Photoactinic answered 15/1, 2013 at 16:30 Comment(2)
Note that this is neither auto-boxing nor auto-unboxing. The @... syntax for scalars and collections is, like the dot syntax, compiler shorthand for a concrete method call. Autoboxing would imply that a bare scalar (int x = 5;) would be magically boxed when passed to a method that requires NSNumber*. (KVC's valueForKey: is auto-boxing / un-boxing, for example).Affenpinscher
Thanks for that, @bbum. Correct to call them "object literals?"Photoactinic
T
8
[oneNumber[0] intValue]

Sometimes the old ways are best.

Tatar answered 15/1, 2013 at 16:58 Comment(2)
I always forget that there are limits to the dot syntax. +1 great pointPhotoactinic
Provided you KNOW in advance that oneNumber[0] responds to intValue call. Else you crash. I agree, however about the "old ways". Too much of a "good thing" may sometimes...Beaulieu
P
0

Another way to go is to stay in the object world. For instance:

NSNumber *one = @1;
NSArray *oneNumber = @[one];
one = oneNumber[0];
NSLog(@"one %@", one);
Photoactinic answered 15/1, 2013 at 17:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.