Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

componentsSeparatedByString() Error in Swift 3

Tags:

swift3

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.

like image 803
Ethan Rappaport Avatar asked Jul 13 '16 17:07

Ethan Rappaport


People also ask

How do you split a string into two parts in Swift?

Swift String split() The split() method breaks up a string at the specified separator and returns an array of strings.

How do you split a string after a specific character in Swift?

To split a String by Character separator in Swift, use String. split() function. Call split() function on the String and pass the separator (character) as argument. split() function returns a String Array with the splits as elements.

How do you split in Swift?

To split a string to an array in Swift by a character, use the String. split(separator:) function. However, this requires that the separator is a singular character, not a string.

How do you split a string in Objective C?

You can also split a string by a substring, using NString's componentsSeparatedByString method. You should be able to use NSString's "componentsSeparatedByCharactersInSet:" to split on multiple characters.


1 Answers

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

like image 84
Steven Hepting Avatar answered Sep 21 '22 22:09

Steven Hepting