iOS 7.1 CommonCrypto 库抱怨:隐式转换失去整数精度:'NSUInteger' (unsigned long) to CC_LONG (unsigned int) [英] iOS 7.1 CommonCrypto library complains: Implicit conversion loses integer precision: 'NSUInteger' (unsigned long) to CC_LONG (unsigned int)

查看:21
本文介绍了iOS 7.1 CommonCrypto 库抱怨:隐式转换失去整数精度:'NSUInteger' (unsigned long) to CC_LONG (unsigned int)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从文件中执行 MD5 时收到上述错误(在标题中).. 我通常可以处理这些类型的 32->64 位转换问题..但在这种情况下,我不知道我应该做什么CC_MD5 是 CommonCrypto->CommonDigest 的一部分,后者是 iOS7.1 附带的库.我假设 [inputData length] 正在返回 NSUInteger,这就是问题所在,但是我可以简单地从 UL 转换为 UI 吗?如果文件很大,我可能会失去精度.为什么 Apple 附带的库需要 int 支持 64 位语言(例如 iOS)?是有人忽略了什么,还是我真的很愚蠢并误诊了问题?

I get the above error (in title) whilst doing a MD5 from file.. I can usually cope with these type of 32->64bit conversion issues..but in this case, I do not know what I should do as CC_MD5 is part of CommonCrypto->CommonDigest, a library that ships with iOS7.1. I am assuming [inputData length] is returning NSUInteger and therein lies the issue, however can I simply cast down from UL to UI? I will possibly lose precision if the file is large. Why would a library that Apple ships with require int in a 64 bit capable language such as iOS? Did someone overlook something or am I being really stupid and mis-diagnosing the problem?

- (NSString*) getMD5FromFile:(NSString *)pathToFile {
    unsigned char outputData[CC_MD5_DIGEST_LENGTH];

    NSData *inputData = [[NSData alloc] initWithContentsOfFile:pathToFile];
    CC_MD5([inputData bytes], [inputData length], outputData);
    [inputData release];

    NSMutableString *hash = [[NSMutableString alloc] init];

    for (NSUInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [hash appendFormat:@"%02x", outputData[i]];
    }

    return [hash autorelease];
}

来自 CommonCrypto->CommonDigest.h:

From CommonCrypto->CommonDigest.h:

extern unsigned char *CC_MD5(const void *data, CC_LONG len, unsigned char *md)

我向 Apple (#17256918) 提交了错误报告,但没有办法在没有错误的情况下做到这一点吗?

I filed bug report with apple (#17256918) but isn't there a way to do this without the error?

推荐答案

您只需要将 [inputData length] 转换为 intuint32_t正如你所怀疑的那样.

You simply need to cast [inputData length] to either int or uint32_t as you suspect.

如果你想断言这个数字对于类型来说不是太大,你可以使用 UINT32_MAX 来检查.这是您可以使用的 NSAssert 示例.

If you want to assert that number is not too large for type you can use the UINT32_MAXto check. Here is an example on an NSAssert you could use.

    NSAssert([inputData length] < UINT32_MAX, @"too big!");

这篇关于iOS 7.1 CommonCrypto 库抱怨:隐式转换失去整数精度:'NSUInteger' (unsigned long) to CC_LONG (unsigned int)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆