int foo = 42;
NSNumber* boxed = @foo; // fails
NSNumber* boxed = @(foo); // works
One thing that isn't supported but would be extremely useful is a shortcut for NSValue boxing. Fortunately, it's really easy to solve using a short macro, e.g.:
I've made that same macro, unfortunately the unboxing is the hard part because you don't know what type the NSValue contains. This is why I was hoping it would be a compiler feature because the compiler could use the surrounding info to figure it out:
int x = some_nsvalue; // it knows you want an int
[obj takeDouble:some_nsvalue]; // it knows takeDouble expects a double
(CGAffineTransform)some_nsvalue // typecast is the ultimate hint
Yeah, that would require type inference via the expression/return value though and not even C++ can do that (by and large anyway). But then ObjC as a language in general doesn't seem to care much for type safety anyway. I realise this makes objects quite flexible, but at the same time it's so, so easy to write code with undefined behaviour or with glaring security issues. I've actually come up with a typed variable binding macro for picking apart untrusted NSDictionaries (primarily from untrusted plists), which when used looks something like this:
This expands into variable declarations and type-checked initialisations from objectForKey: method calls which otherwise are very messy. You could define something similar for extracting and binding from NSValues in one step, though none of this is very elegant.
So a number of things. With LLVM, Objective-C has become way stricter and picky about types. To get away with calling a random method on an object ObjC knows doesn't have it, you have to explicitly cast it. This was necessary to ensure ARC would work correctly. Although, yeah, in the dictionary example it just returns an id so its up to you to type it correctly.
That being said, that doesn't have much to do with what I'm talking about because here I'm referring specifically to NSValue -> primitive type conversion. Just about every compiler is super picky about this. Try doing this in Xcode:
void * p;
int x = p;
It will get angry, it knows p is a pointer and you want an int. Couple this with Xcode also knowing whether something is just id or actually an NSValue, and you can then do this:
id something = [object returnIdSoYouDontWhatItReallyIs];
int somethingAsInt = something; // error, something can't be converted to int
However, here it should be fine:
NSValue * something = [object returnsSomethingMysterious];
int somethingAsInt = something; // OK, replace with something.intValue
Of course, you'd probably want some sort of explicit unboxing operator (to match the explicit boxing operator @()), because sometimes you do want to do crazy things like cast a id to an int. Maybe something like: @^()
The functionality is present for iOS, it just isn't being exposed. If you create a category header file (just header, not implementation) the compiler'll pick up on it and all will work fine.
Apparently OSX 10.8 and iOS 6 bring support for subscripting to native NSArray/NSDictionary, but I think you can add subscripts to your own classes already just by implementing the following methods:
PS: Not sure if the object subscripting stuff is supported yet, but that looks awesome too!