Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What NSNumber (Integer 16, 32, 64) in Core Data should I use to keep NSUInteger

I want to keep NSUInteger into my core data and I don't know which type should I use (integer 16, 32, 64) to suit the space needed.

From my understanding:

Integer 16 can have minimum value of -32,768 to 32,767 Integer 32 can have minimum value of -2,147,483,648 to 2,147,483,647 Integer 64 can have minimum value of -very large to very large 

and NSUInteger is type def of unsigned long which equal to unsigned int (Types in objective-c on iPhone)

so If I convert my NSUInteger to NSNumber with numberWithUnsignedInteger: and save it as NSNumber(Integer 32) I could retrieve my data back safely right?

like image 938
sarunw Avatar asked Jan 13 '12 16:01

sarunw


People also ask

What is NSNumber in objective C?

NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char , short int , int , long int , long long int , float , or double or as a BOOL .

Is NSInteger primitive?

It is not a C primitive (like int, unsigned int, float, double, etc.) NSInteger , CGFloat , NSUInteger are simple typedefs over the C primitives. The need for NSNumber arises from the need to use numbers as parameters to APIs that require Objects.


1 Answers

Do you really need the entire range of an NSUInteger? On iOS that's an unsigned 32 bit value, which can get very large. It will find into a signed 64 bit.

But you probably don't need that much precision anyway. The maximum for a uint32_t is UINT32_MAX which is 4,294,967,295 (4 billion). If you increment once a second, it'll take you more than 136 years to reach that value. Your user's iPhone won't be around by then... :)

like image 114
Daniel Eggert Avatar answered Sep 18 '22 17:09

Daniel Eggert