Swift 3中的componentsSeparatedByString()错误 [英] componentsSeparatedByString() Error in Swift 3
问题描述
var numbers = "Hello,Goodbye,Hi,Bye"
var numbersArr = numbers.componentsSeparatedByString(",")
//["Hello"."Goodbye","Hi","Bye"]
以上是我要执行的操作的基本表示.我正在尝试使用componentsSeparatedByString()
将带有逗号的字符串拆分为一个数组,其中数组的每个组件都位于原始字符串的每个逗号之间.
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.
我正在使用IBM Swift Sandbox(对不起,我在Windows上:)),在Swift 3.0中,我收到此错误消息:
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'
我知道Swift 3相当新,这就是为什么我找不到此错误的任何其他引用的原因.
I know Swift 3 is rather new, and is that is why I couldn't find ANY other references for this error.
推荐答案
在String
上似乎有一个components(separatedBy:)
:
import Foundation
let words = "apple binary cat delta echo".components(separatedBy: " ")
print(words)
IBM Playground链接: http://swiftlang.ng.bluemix.net/#/repl/57868332b4e4e9971bf9f4e8
IBM Playground link: http://swiftlang.ng.bluemix.net/#/repl/57868332b4e4e9971bf9f4e8
这篇关于Swift 3中的componentsSeparatedByString()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!