I know I can do @3
instead of [NSNumber numberWithInt:3]
but what's the literal for [NSNumber numberWithUnsignedInteger:3]
?
Asked
Active
Viewed 1,445 times
4
3 Answers
8
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.

dreamlax
- 93,976
- 29
- 161
- 209
-
Looks like you can also do lowercase suffixes, e.g., @3u, according to Xcode Docs : Programming with Objective-C : Values and Collections. – ma11hew28 Sep 14 '12 at 00:48
5
Typecasting also works if you do not remember the correct literal.
NSNumber* number = @((NSUInteger)3);

Jonathan Grynspan
- 43,286
- 8
- 74
- 104

Davyd Geyl
- 4,578
- 1
- 28
- 35
-
`+numberWithUnsignedInteger:` takes `NSUInteger`, not `unsigned int`. I have edited your post to reflect that. – Jonathan Grynspan Sep 13 '12 at 01:35
-
Actually, this seems better than @3u because it's correct on both Mac & iOS, whereas adding u as a suffix is only correct for iOS, not Mac (ul). Correct? – ma11hew28 Sep 14 '12 at 01:03
2
@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
.

Jonathan Grynspan
- 43,286
- 8
- 74
- 104
-
1Actually, 3 is not representable by a 1 wide bit-field, FYI. – Richard J. Ross III Sep 13 '12 at 01:31
-
Is `_Bool` a numeric type? I'm quite sure conversion rules for `_Bool` ensure it only holds 0 or 1. – dreamlax Sep 13 '12 at 04:52
-
-
I'm only teasing :) your answer has a valid point, but perhaps `3UL` is not the only unsigned integer requiring boxing. – dreamlax Sep 13 '12 at 05:17
-
@dreamlax: They all require boxing to be used as an `NSNumber`, but worrying about the type of the primitive is generally silly since `NSNumber` will convert losslessly wherever possible. – Jonathan Grynspan Sep 13 '12 at 05:25
-
-
-
-
@MattDiPasquale: You misunderstand. One will generate an `unsigned long` while the other will just generate an `unsigned int`. But *it doesn't matter* because the primitive type of the value is abstracted away by `NSNumber`, which then can be asked for any numeric type you need. You only need to worry if the value cannot be represented by both primitive types. – Jonathan Grynspan Sep 19 '12 at 12:29
-
@MattDiPasquale: If you're referring to the definition of `NSUInteger` as `unsigned int` on some targets, it's still moot because `unsigned int` and `unsigned long` are explicitly identical types under the relevant ABIs, so supplying a value of one type to a function/etc. expecting the other is safe/free. – Jonathan Grynspan Sep 19 '12 at 12:32
-
OK. To be safe (& explicit), I prefer to just cast as `NSUInteger` as @Davyd suggests. – ma11hew28 Sep 19 '12 at 13:44
-
@MattDiPasquale: Actually, Davyd's suggestion won't give you a compile-time constant, it'll give you a runtime box. But otherwise, it's not really going to make a difference. – Jonathan Grynspan Sep 19 '12 at 17:05