componentsSeparatedByString() Error in Swift 3

2019-03-22 14:16发布

var numbers = "Hello,Goodbye,Hi,Bye"
var numbersArr = numbers.componentsSeparatedByString(",")

//["Hello"."Goodbye","Hi","Bye"]

Above is a basic representation of what I'm trying to do. I'm trying to use componentsSeparatedByString() to split a string with commas into an array, where each of the components of the array are between each of the commas from the original strings.

I am using IBM Swift Sandbox (Sorry, I'm on windows :) ), and in Swift 3.0, I am getting this error message:

value of type 'String' has no member 'componentsSeparatedByString'

I know Swift 3 is rather new, and is that is why I couldn't find ANY other references for this error.

标签: swift3
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-22 14:40

It looks like there is a components(separatedBy:) on String:

import Foundation

let words = "apple binary cat delta echo".components(separatedBy: " ")
print(words)

enter image description here

IBM Playground link: http://swiftlang.ng.bluemix.net/#/repl/57868332b4e4e9971bf9f4e8

查看更多
登录 后发表回答