Xcode-beta (7A165t): Allow Getting NSData from a UTF8View on a Swift String
| Originator: | john | ||
| Number: | rdar://21994199 | Date Originated: | 24-Jul-2015 11:04 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode-beta (7A165t) |
| Classification: | Feature (New) | Reproducible: | Always |
Summary: When serializing a String to some other output stream, it can be necessary to get an NSData object representing its encoded bytes. Currently the only way to do this is to call dataUsingEncoding, which returns nil. This is frustrating when using the UTF-8 encoding, since any valid string can be encoded into UTF-8, but the compiler has no static knowledge of that. This means that the code either has to contain a forced unwrapped optional, a fallback to a dummy value, or a guard statement with a fatal error. All of those seem like code smells. It seems like a better alternative would be to get the NSData object from the String’s UTF-8 view, which has static knowledge that it can be converted into a byte stream safely. One option for making this work would be to add a method to UTF8View for getting the data, but another way that could pay more dividends is adding an initializer to NSData to initialize it from a collection of UInt8 objects. This would also allow creating NSData objects from byte arrays without having to go through the UnsafePointer iniitalizer. Steps to Reproduce: Expected Results: Actual Results: Regression: Notes:
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!