The second case is simpler because array2 is a UnicodeScalarView and not an array:
let array2 = "bar".unicodeScalars let str2 = String(array2) print(str2)
If you have an array (or any sequence) of Unicode scanners, you can start with an empty line and add elements to the unicodeScalars :
let array = [UnicodeScalar("f")!, UnicodeScalar("o")!, UnicodeScalar("o")!] // Or: let array: [UnicodeScalar] = ["f", "o", "o"] var str1 = "" str1.unicodeScalars.append(contentsOf: array) print(str1) // foo
Of course, you can define a custom extension for this purpose:
extension String { init<S: Sequence>(unicodeScalars ucs: S) where S.Iterator.Element == UnicodeScalar { var s = "" s.unicodeScalars.append(contentsOf: ucs) self = s } } let str1 = String(unicodeScalars: array)
source share