Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the split function in Swift 2

Tags:

swift2

Let's say I want to split a string by an empty space. This code snippet works fine in Swift 1.x. It does not work in Swift 2 in Xcode 7 Beta 1.

var str = "Hello Bob" var foo = split(str) {$0 == " "} 

I get the following compiler error:

Cannot invoke 'split' with an argument list of type '(String, (_) -> _) 

Anyone know how to call this correctly?

Updated: Added a note that this was for the Xcode 7 beta 1.

like image 339
Mallioch Avatar asked Jun 10 '15 14:06

Mallioch


People also ask

How do you split 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 I separate a string from a character in Swift?

You can use components(separatedBy:) method to divide a string into substrings by specifying string separator. let str = "Hello! Swift String."

How do you use the flow function to split?

Description. The Split function breaks a text string into a table of substrings. Use Split to break up comma delimited lists, dates that use a slash between date parts, and in other situations where a well defined delimiter is used. A separator string is used to break the text string apart.

How do I convert a string to an array in Swift?

To convert a string to an array, we can use the Array() intializer syntax in Swift. Here is an example, that splits the following string into an array of individual characters. Similarly, we can also use the map() function to convert it. The map() function iterates each character in a string.


2 Answers

split is a method in an extension of CollectionType which, as of Swift 2, String no longer conforms to. Fortunately there are other ways to split a String:

  1. Use componentsSeparatedByString:

    "ab cd".componentsSeparatedByString(" ") // ["ab", "cd"] 

    As pointed out by @dawg, this requires you import Foundation.

  2. Instead of calling split on a String, you could use the characters of the String. The characters property returns a String.CharacterView, which conforms to CollectionType:

    "😀 🇬🇧".characters.split(" ").map(String.init) // ["😀", "🇬🇧"] 
  3. Make String conform to CollectionType:

    extension String : CollectionType {}  "w,x,y,z".split(",") // ["w", "x", "y", "z"] 

    Although, since Apple made a decision to remove String's conformance to CollectionType it seems more sensible to stick with options one or two.


In Swift 3, in options 1 and 2 respectively:

  • componentsSeparatedByString(:) has been renamed to components(separatedBy:).
  • split(:) has been renamed to split(separator:).
like image 176
ABakerSmith Avatar answered Sep 19 '22 05:09

ABakerSmith


Swift 4

let str = "Hello Bob" let strSplitArray = str.split(separator: " ") strSplitArray.first!    // "Hello" strSplitArray.last!     // "Bob" 

Xcode 7.1.1 with Swift 2.1

let str = "Hello Bob" let strSplit = str.characters.split(" ") String(strSplit.first!) String(strSplit.last!) 
like image 25
Warif Akhand Rishi Avatar answered Sep 22 '22 05:09

Warif Akhand Rishi