5

如何在 Objective-C 中将 int64_t 转换为 NSInteger?

这个方法返回一个 int64_t* 的分数,我需要将它转换为 NSInteger:

[OFHighScoreService getPreviousHighScoreLocal:score forLeaderboard:leaderboardId];

谢谢你。

4

2 回答 2

7

它们应该在 64 位机器上直接兼容(或者如果您使用 构建NS_BUILD_32_LIKE_64):

NSInteger i = *score;

文档表明 的定义如下NSInteger所示:

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

因此,在 32 位机器上,您可能需要处理一些截断问题。在我的机器上,这个语句:

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

给出这个输出:

2010-03-19 12:30:18.161 app[30675:a0f] 8 8
于 2010-03-19T19:29:17.997 回答
2

问题出在我的代码上:

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

工作应该是:

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

有了这段代码,我显然可以做到:

NSInteger newScore = score;

谢谢你。

于 2010-03-20T16:46:48.203 回答