【问题标题】:Convert int64_t to NSInteger将 int64_t 转换为 NSInteger
【发布时间】:2010-03-19 19:22:58
【问题描述】:

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

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

[OFHighScoreService getPreviousHighScoreLocal:score forLeaderboard:leaderboardId];

谢谢。

【问题讨论】:

  • 40亿还不够高分?这是一个疯狂的框架。

标签: objective-c casting nsinteger


【解决方案1】:

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

NSInteger i = *score;

documentation 表示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

【讨论】:

    【解决方案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;
    

    谢谢。

    【讨论】:

    • 这没有回答“将 int64_t 转换为 NSInteger”的问题。
    猜你喜欢
    • 2011-06-17
    • 2011-09-21
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多