簡體   English   中英

隱式轉換失去整數精度:'long long'到'NSInteger'(又名'int')

[英]Implicit conversion loses integer precision: 'long long' to 'NSInteger' (aka 'int')

我試圖將類型為“long long”的變量賦值給NSUInteger類型,這樣做的正確方法是什么?

我的代碼行:

expectedSize = response.expectedContentLength > 0 ? response.expectedContentLength : 0;

其中expectedSize的類型為NSUInteger,返回類型為response.expectedContentLength ,類型為“ long long ”。 變量response的類型為NSURLResponse

顯示的編譯錯誤是:

語義問題:隱式轉換失去整數精度:'long long'到'NSUInteger'(又名'unsigned int')

你可以嘗試使用NSNumber進行轉換:

  NSUInteger expectedSize = 0;
  if (response.expectedContentLength) {
    expectedSize = [NSNumber numberWithLongLong: response.expectedContentLength].unsignedIntValue;
  }

它實際上只是一個演員,有一些范圍檢查:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM