if let encoding = String.Encoding(rawValue: 999) {
// ...
}
Produces a compiler error saying "Initializer for conditional binding must have Optional type, not 'String.Encoding'" because despite the docs saying the String.Encoding initializer is failable, it is not and will happily create non-existent encodings.
How do I check if the encoding returned by initializer is an actual encoding?
The two ideas I have are
- Check the String.Encoding description is not empty. This assumes that supported encodings must have a description
- Encode something - e.g.
"abc".data(using: encoding) == nil- which assumes that the string "abc" can be encoded by all supported encodings