+[NSNumber numberWithUnsignedInteger:] Literal?
-
01-07-2021 - |
Question
I know I can do @3
instead of [NSNumber numberWithInt:3]
but what's the literal for [NSNumber numberWithUnsignedInteger:3]
?
Solution
Typecasting also works if you do not remember the correct literal.
NSNumber* number = @((NSUInteger)3);
OTHER TIPS
You can use @3U
or @3UL
(or use the usual integer suffixes to change the type, e.g. @3ULL
for unsigned long long
).
See this page for more information on NSNumber literals.
@3UL
will give you an NSNumber
that is explicitly the same width/signedness as an NSUInteger
.
But ask yourself why you want this? Since 3
is representable exactly by all numeric types in C/Objective-C1, @3
gives you an object that behaves identically. Although the type of the constant used is different, the object you get can be used in all the same ways and can be unboxed with the same set of methods.
1 Okay, okay, excepting types that represent only 1 bit such as _Bool
and int : 1
.