Convert int64_t to NSInteger

How to convert int64_t to NSInteger in Objective-C?

This method returns int64_t * into the account, and I need to convert it to NSInteger:

[OFHighScoreService getPreviousHighScoreLocal:score forLeaderboard:leaderboardId]; 

Thanks.

+4
source share
2 answers

The problem was in my code:

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

To work, it must be:

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

And with this code, I obviously can do:

 NSInteger newScore = score; 

Thanks.

+2
source

They must be directly compatible on a 64-bit machine (or if you create using NS_BUILD_32_LIKE_64 ):

 NSInteger i = *score; 

The documentation indicates that the NSInteger definition is as follows:

 #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 may have problems with truncation. On my machine here is this statement:

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

gives this result:

 2010-03-19 12:30:18.161 app[30675:a0f] 8 8 
+6
source

Source: https://habr.com/ru/post/1304603/


All Articles