In order to convert a String
instance to a Data
instance in Swift you can use data(using:allowLossyConversion:)
, which returns an optional Data
instance.
Can the return value of this function ever be nil
if the encoding is UTF-8 (String.Encoding.utf8
)?
If the return value cannot be nil
it would be safe to always force-unwrap such a conversion.