Auto Boxing of primitives

objective-c

Solution

Unfortunately, Objective-C does not do auto-boxing or unboxing of primitive types to `NSNumber`. When put that way, it may be clear why: Objective-C has no concept of `NSNumber`, a class in the Cocoa Foundation framework. As a small superset of C, Objective-C doesn't have a "native" numeric object type--just the native C types.

Edit Aug 2012 As of Xcode 4.4 (and LLVM 4.0), you can now use some syntactic sugar to wrap numbers. Following your example, these "boxed expressions" now work:

float foo = 12.5f;
NSNumber* bar;

bar = @(foo);
bar = @12.5f;

Problem

I can't seem to figure out how to get Objective-c to auto box my primitives. I assumed that i would be able to do the following ``` float foo = 12.5f; NSNumber* bar; bar = foo; ``` However i find that i have used to the more verbose method of ``` float foo = 12.5f; NSNumber* bar; bar = [NSNumber numberWithFloat:foo]; ``` Am i doing it wrong or is this as good as it gets?

Original source