NSString(bytes:length:encoding) is not equivalent to String
| Originator: | robnapier | ||
| Number: | rdar://21709582 | Date Originated: | 07-Jul-2015 03:41 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Swift 2b2 |
| Classification: | Other Bug | Reproducible: | Always |
In Swift, NSString methods are generally available on String, but NSString(bytes:length:encoding) is not, and leads to surprising behavior: let data = [UInt8]([65]) let string = String(bytes: data, length: data.count, encoding: NSUTF8StringEncoding) let nsstring = NSString(bytes: data, length: data.count, encoding: NSUTF8StringEncoding) as? String “string” is of type String and is “([65], 1,4)” because it is interpreted as a tuple and passed to String(_ instance:). “nsstring” is of type String? containing Some(“A”) (as expected)
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!