String.split(separator: ";") crashes preview in SwiftUI

When I run the following code, the preview of my view crashes.

let data = "123;456;"
let dataArr = data.split(separator: ";")


The Diagnostics always show the following:


argument type 'String.Element' (aka 'Character') does not conform to expected type 'ExpressibleByStringLiteral'

let dataArr = data.split(separator: __designTimeString("#9734.[1].[4].[0].value.modifier[0].arg[0].value", fallback: ";"))


Does anyone know how to fix this?

Accepted Reply

Should try:


let mySeparator : Character = ";"
let data = "123;456;"
let dataArr = data.split(separator: mySeparator)


or, simply:

let data = "123;456;"
let dataArr = data.split(separator: Character(";"))


You could also file a bug report.


Note: this is not an IDE topic, more a SwiftUI or even just plain Swift topic.

Replies

Should try:


let mySeparator : Character = ";"
let data = "123;456;"
let dataArr = data.split(separator: mySeparator)


or, simply:

let data = "123;456;"
let dataArr = data.split(separator: Character(";"))


You could also file a bug report.


Note: this is not an IDE topic, more a SwiftUI or even just plain Swift topic.

If you want to allow several separators, use:


let separators = CharacterSet(charactersIn: "; ,")          // semi colon, space and comma will be allowed
let data = "123;456 789,ABCD"
let dataArr = data.components(separatedBy: separators)
print(dataArr)

which gives


["123", "456", "789", "ABCD"]