简体   繁体   中英

NSData length - Implicit conversion loses integer precision

I have a little problem that I do not understand.

I looked on the Internet but I do not see where is my mistake.

id manufacturerData = [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];
if (manufacturerData) {
    const uint8_t *bytes = [manufacturerData bytes];
    int len = [manufacturerData length];
    // skip manufacturer uuid
    NSData *data = [NSData dataWithBytes:bytes+2 length:len-2];

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

Note that your manufacturerData variable's type should be NSData , not id .

Look at the docs for NSData . What is the return type for the length property? Now look at the type you are using. See the difference?

Always use the proper data type.

You can also use the subdataWithRange: method instead of using the bytes.

NSData *manufacturerData = [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];
if (manufacturerData) {
    NSUInteger len = [manufacturerData length];
    // skip manufacturer uuid
    NSRange *dataRange = NSMakeRange(2, len - 2);
    NSData *data = [manufacturerData subdataWithRange:dataRange];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM