iosobjective-carmnsuinteger

Handling 64 bit integers on a 32 bit iPhone


I would like to handle 64 bit unsigned integers on a iPhone 4s (which of course has a 32 bit ARM 6 processor).

When trying to work with 64 bit unsigned integers, e.g. Twitter IDs, I have the following problem:

// Array holding the 64 bit integer IDs of the Tweets in a timeline:
NSArray *Ids =[timelineData valueForKeyPath:@"id"];

// I would like to get the minimum of these IDs:
NSUInteger minId = (NSUInteger) Ids.lastObject;

The array Ids contains the following numbers (= Tweet Ids):

491621469123018752,
491621468917477377,
491621465544851456,
491621445655867393

However, minId returns the incorrect value of 399999248 (instead of 491621445655867393)

How can I find the minimum or the last object in an Array of 64 bit integers on an iPhone 4s?


Solution

  • You need to use a type that is always 64 bit instead of NSUInteger. You can use uint64_t or unsigned long long. You also need to get the integer value out of the NSNumber (arrays can't store C types). to do this you need to call

    uint64_t minID = [Ids.lastObject longLongValue];
    

    Edit

    Changed to use uint64_t in example code as it has been correctly pointed out this shows your intent better.