Bogdan Bogdanov
Bogdan Bogdanov

Reputation: 992

Swift - Character is not a subtype of string

myText = "word 1 / word 2"    
var testVar = split(myText, { $0 == "/"}, maxSplit: Int.max, allowEmptySlices: false)

This code works but it takes empty space "word 1 " when I use testVar[0]

when I write empty spaces

var testVar = split(myText, { $0 == " / "}, maxSplit: Int.max, allowEmptySlices: false)

I get an error: 'Character' is not a subtype of 'String'

Anyone who knows how to fix that?

Upvotes: 0

Views: 537

Answers (1)

Nate Cook
Nate Cook

Reputation: 93276

The split() function only works on Swift strings by comparing each element of the string as a Character. To use a string to split a string, use .componentsSeparatedByString:

var testVar = myText.componentsSeparatedByString(" / ")

Upvotes: 2

Related Questions