简体   繁体   中英

Incompatible pointer types assigning to 'NSMutableData *' from 'NSData *'

I am getting ERROR

"Incompatible pointer types assigning to 'NSMutableData *' from 'NSData *'"

As per my knowledge it must be a warning instead of Error. So i think is there any issue with my Xcode Project setting?

Here is the image of the error. 在此处输入图片说明

It should be an error, because it will crash at runtime when someone gets the apparently mutable data and tries to mutate it.

Basically, you shouldn't be doing what you're doing. Use mutableCopy to ensure that the data is mutable, or, as you're calling a constructor, make sure you call it on NSMutableData .

It looks like your self.activeDownload is declared as type NSMutableData, so the compiler is letting you know that the assignment is not valid (ie you can't mutate the data after the assignment). Simply change the NSData to NSMutableData .

self.activeDownload = [NSMutableData dataWithContentsOfFile:filePath];

You're lucky that it is an error. Only totally inexperienced programmers would make this just a warning. If there's a bug in your code, would you rather that the compiler tells you or that you have to start debugging?

NSMutableData means the object can be changed. Assigning an NSData object means your code now thinks the object can be changed, but if you try, you'll get a crash at runtime. Is that really what you want?

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