我试图将一个类型为' long‘的变量赋值给NSUInteger类型,那么正确的方法是什么呢?
我的代码行:
expectedSize = response.expectedContentLength > 0 ? response.expectedContentLength : 0;其中expectedSize的类型是NSUInteger,而返回类型的response.expectedContentLength是'long long‘类型。变量response为NSURLResponse类型。
显示的编译错误是:
语义问题:隐式转换失去整数精度:“长长”到“NSUInteger”(又名“无符号int”)
发布于 2012-05-16 10:46:04
它实际上只是一个演员,有一些范围检查:
const long long expectedContentLength = response.expectedContentLength;
NSUInteger expectedSize = 0;
if (NSURLResponseUnknownLength == expectedContentLength) {
assert(0 && "length not known - do something");
return errval;
}
else if (expectedContentLength < 0) {
assert(0 && "too little");
return errval;
}
else if (expectedContentLength > NSUIntegerMax) {
assert(0 && "too much");
return errval;
}
// expectedContentLength can be represented as NSUInteger, so cast it:
expectedSize = (NSUInteger)expectedContentLength;发布于 2012-05-16 10:30:46
您可以尝试使用NSNumber进行转换:
NSUInteger expectedSize = 0;
if (response.expectedContentLength) {
expectedSize = [NSNumber numberWithLongLong: response.expectedContentLength].unsignedIntValue;
}https://stackoverflow.com/questions/10615950
复制相似问题