Apple's documentation and many articles suggest you can convert a substring (string.subsequence) to a string just by calling String(substring)and in fact this works.
let str = "Hello world"
let hello = str.prefix(5) //create substr
//CREATE STRING FROM SUBSTRING
let hellostr = String(hello) //works
However when the substring is an optional as in the following,
let str = Optional("Hello world")
let hello = str?.prefix(5) //create substr
//CREATE STRING FROM OPTIONAL SUBSTRING
let hellostr = String(hello) //gives error
I am getting the error
"No exact matches in call to initializer "
What could be causing this error?
Thanks for any suggestions.
init(_ substring: Substring), notinit(_ substring: Substring?). SoString(hello ?? Substring())should do the trick. Also the comments about the code " //gives error " is false and misleading (I guess wrong copy/paste)nil? Practically there is none therefore aninitmethod is pointless.