swift tips

Can encoding a String to Data with utf8 fail?

Surely none of us programming in Swift likes force-unwrapping optional values too much (we prefer a ‘guard’ – although excessive use is also not quite the right approach).

Quick reminder for beginners: optionals represent data that may or may not be, but sometimes you know for sure that the value is not nil. In these cases, Swift lets you force unwrap the optional: transforming it from an optional type to a non-optional type.

I am sure many of you have encountered or used the following construction:

"some string".data(using: .utf8)!

Are you sure we can unwrap it?

In theory, UTF-8 can represent all valid Unicode code points, so converting a Swift string to UTF-8 data cannot fail.

But are you sure?

I recently came across the following example:

let x = "🙃" as NSString
// this will create a broken string using UTF16 NSString offsets, which is illegal here because we are splitting within one surrogate pair.
let broken = x.substring(with: NSRange(location: 0, length: 1)).data(using: .utf8)

So, if you want something that can’t fail, use String.utf8, which gives a UTF8View that will use a Unicode character substitution if you have any illegal strings:

let data = Data("some string".utf8)

In Swift 5.1 some improvements have been made, so someString.data(using: .utf8)! already works the same way, but why unwrap and waste someone’s time thinking about it in code review.

Leave a Reply

Your email address will not be published.