When converting String to Data, the most common answer online is use String.data(using:)
.
if let data = string.data(using: .utf8) {
// process the data
}
Why not use String.utf8
to convert the string into a String.UTF8View
collection and initialize a Data
instance with Data.init(_:)
?
let data = Data(string.utf8)
// process the data
Neither String.utf8
or Data.init(_:)
return an optional, so no need for optional binding.