Convert int64_t to NSInteger
Asked Answered
L

2

5

How can i convert int64_t to NSInteger in Objective-C ?

This method returns into score an int64_t* and I need to convert it to NSInteger:

[OFHighScoreService getPreviousHighScoreLocal:score forLeaderboard:leaderboardId];

Thank you.

Loney answered 19/3, 2010 at 19:22 Comment(1)
4 billion is not enough for highscore? That's an insane framework.Fiore
L
2

The problem was on my code:

int64_t *score; 
[OFHighScoreService getPreviousHighScoreLocal:score forLeaderboard:leaderboardId];
NSLog(@"------------------------- %d", *score);

To work it should be:

int64_t score;  
[OFHighScoreService getPreviousHighScoreLocal:&score forLeaderboard:leaderboardId];
NSLog(@"------------------------- %qi", score);

And with this code i can obviously do:

NSInteger newScore = score;

Thank you.

Loney answered 20/3, 2010 at 16:46 Comment(1)
This does not answer the question "Convert int64_t to NSInteger".Patsis
B
7

They should be directly compatible on a 64-bit machine (or if you build with NS_BUILD_32_LIKE_64):

NSInteger i = *score;

The documentation indicates that the definition of NSInteger looks like this:

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
#else
typedef int NSInteger;
#endif

So on a 32-bit machine you might have some truncation issues to deal with. On my machine here, this statement:

NSLog(@"%d %d", sizeof(int64_t), sizeof(NSInteger));

gives this output:

2010-03-19 12:30:18.161 app[30675:a0f] 8 8
Bren answered 19/3, 2010 at 19:29 Comment(0)
L
2

The problem was on my code:

int64_t *score; 
[OFHighScoreService getPreviousHighScoreLocal:score forLeaderboard:leaderboardId];
NSLog(@"------------------------- %d", *score);

To work it should be:

int64_t score;  
[OFHighScoreService getPreviousHighScoreLocal:&score forLeaderboard:leaderboardId];
NSLog(@"------------------------- %qi", score);

And with this code i can obviously do:

NSInteger newScore = score;

Thank you.

Loney answered 20/3, 2010 at 16:46 Comment(1)
This does not answer the question "Convert int64_t to NSInteger".Patsis

© 2022 - 2024 — McMap. All rights reserved.