Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to assign a variable with type 'long long' to a type NSUInteger, What is the correct way to do that?

my code line:

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

where expectedSize is of type NSUInteger and return type of response.expectedContentLength is of type 'long long'. The variable response is of type NSURLResponse.

The compile error shown is:

Semantic Issue: Implicit conversion loses integer precision: 'long long' to 'NSUInteger' (aka 'unsigned int')

like image 973
Firdous Avatar asked May 16 '12 09:05

Firdous


2 Answers

you could try the conversion with NSNumber:

  NSUInteger expectedSize = 0;
  if (response.expectedContentLength) {
    expectedSize = [NSNumber numberWithLongLong: response.expectedContentLength].unsignedIntValue;
  }
like image 75
CarlJ Avatar answered Oct 03 '22 14:10

CarlJ


It's really just a cast, with some range checking:

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;
like image 25
justin Avatar answered Oct 03 '22 14:10

justin