2

I am trying to change syntax "componentsSeparatedByCharactersInSet" to swift 3. But I does not work. How could it adapt to swift 3? This is my code in ios 9. It showed "Value of type 'String' has no member of 'componentsSeparatedByCharactersInSet'.

var delimiter = NSCharacterSet(charactersIn: ",")
func parseHeaders(fromLines lines: [String]) -> [String] {
    return lines.componentsSeparatedByCharactersInSet(self.delimiter)
}
Andyopf
  • 441
  • 1
  • 5
  • 12
  • One of the answers here has the Swift 3 way of doing it: [Swift: Split a String into an array](http://stackoverflow.com/questions/25678373/swift-split-a-string-into-an-array) – TylerP Dec 08 '16 at 18:42
  • 1
    @MrSaturn Thank you. I have just found the syntax accidentally. I can use 'lines.components(separatedBy: self.delimiter as CharacterSet)' to solve this. – Andyopf Dec 08 '16 at 19:08
  • 1
    All you needed to do was look at the documentation for `String`. – rmaddy Dec 08 '16 at 19:20
  • 2
    And since you are using Swift 3, don't use `NSCharacterSet`. Use `CharacterSet`. – rmaddy Dec 08 '16 at 19:20

1 Answers1

7

I found an answer already. Thanks.

func parseHeaders(fromLines lines: [String]) -> [String] {
    return lines.components(separatedBy: self.delimiter as CharacterSet)
}
Andyopf
  • 441
  • 1
  • 5
  • 12