Can the conversion of a String to Data with UTF-8 encoding ever fail?
In order to convert a
String instance to a
Data instance in Swift you can use
data(using:allowLossyConversion:), which returns an optional
Can the return value of this function ever be
nil if the encoding is UTF-8 (
If the return value cannot be
nil it would be safe to always force-unwrap such a conversion.
Solutions Collect From Internet About “Can the conversion of a String to Data with UTF-8 encoding ever fail?”
UTF-8 can represent all valid Unicode code points, therefore a conversion
of a Swift string to UTF-8 data cannot fail.
The forced unwrap in
let string = "some string .." let data = string.data(using: .utf8)!
The same would be true for
.utf32, but not for
encodings which represent only a restricted character set,
You can alternatively use the
.utf8 view of a string to create UTF-8 data,
avoiding the forced unwrap:
let string = "some string .." let data = Data(string.utf8)
- How to always visible scroller of Tableview in Obj c?
- CoreAnimation, AVFoundation and ability to make Video export
- Loop through all object properties at runtime
- Draw shadow on top of UITableViewCell
- Xcode Framework: Umbrella header for module 'Test' does not include header 'NewTest.h'
- How to detect rotation for a programatically generated UIView
- How to resize UIBarButtonItem in code
- XCode compiler doesn't set my variable (and pointer) initial value to 0 or nil!
- Getting Image from URL Objective C
- Programatically create and position an NSButton in an OS X app?
- Swift Framework with libxml
- UITableView: How to change cell height dynamically when a button is clicked in it?
- Implementing object mapping using RestKit in Objective-C
- Swift (beta 3) “NSDictionary? does not conform to protocol 'Equatable'”
- Best method to get text to blink in iPhone OS?