[英]Convert int64_t to NSInteger
如何在Objective-C中將int64_t轉換為NSInteger?
這個方法返回得分為int64_t *,我需要將其轉換為NSInteger:
[OFHighScoreService getPreviousHighScoreLocal:score forLeaderboard:leaderboardId];
謝謝。
它們應該在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
問題出在我的代碼上:
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;
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.