NSCharacterSet objects respond to NSMutableCharacterSet calls incorrectly.

Originator:timothy.sanders
Number:rdar://16268063 Date Originated:2014-03-07
Status:Closed Resolved:Yes.
Product:iOS SDK Product Version:7.0
Classification:Other Bug Reproducible:Always
 
EDITED TO ADD: I just checked this and it is fixed in Xcode 6 beta 7. Now assigning the result of [NSCharacterSet decimalDigitCharacterSet] to a NSMutableCharacterSet * correctly generates a warning. Thanks!

Summary:
The following code will not create any compile errors, raise any assertions, return an error code, or even log a warning message to the console. It also won't do what the developer intended.

NSMutableCharacterSet* fakeMutableSet = [NSCharacterSet decimalDigitCharacterSet];
[fakeMutableSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];

Obviously the code should call mutableCopy in the first line. That bug is on me. But the fact that formUnionWithCharacterSet can fail with no way to tell is troubling.

Steps to Reproduce:
1. Download the attached sample project.
2. Build it (you can repro the problem on the simulator as well as every hardware platform I've tried it on.)
3. Run the app and inspect the console log.

Expected Results:
The underlying problem appears to be that both NSMutableCharacterSet and NSCharacterSet objects are really _NSCFCharacterSet objects. I don't *LIKE* that assigning a non-mutable set to a NSMutableSet pointer works, but I can live with that. At a minimum though I'd like to see the formUnionWithCharacterSet code that runs and *doesn't do what it says it does* at least print a message to the console.

Actual Results:
The runtime gives no indication at all that this has failed. I understand the problem and I don't know any way to code defensively against this, short of actually verifying the contents of the set changed.

Version:
This did not happen on iOS 5. I think the behavior either changed in iOS 6.0 or possibly in a point rev to iOS 6. It's present in 7, I have not yet tried it on a 7.1 beta.

Notes:
I've published the test application on GitHub: https://github.com/HiddenJester/MutableTestApp

Configuration:
Happens all the time in iOS 7. I believe the problem extends to many of the NSMutableSet methods but I haven't built a comprehensive list.

Attachments:
'MutableTestApp.zip' was successfully uploaded.

Update 2014-09-05:

I just checked this and it is fixed in Xcode 6 beta 7. Now assigning the result of [NSCharacterSet decimalDigitCharacterSet] to a NSMutableCharacterSet * correctly generates a warning. Thanks!

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!