1

I'm trying to write my first Swift program, and I know this question has been asked before, but the answers using split aren't working for me. I'm using Xcode 6.4 and Swift 1.2.

I have a String named line.

If I write

 let inputs = split(line) {$0 = " "} 

as suggested at Swift: Split a String into an array, I get the error message "Cannot invoke 'split' with an argument list of type (String, ()->)"

If I write

let inputs = split(line, {find(" ",$0)  != nil}, allowEmptySlices: false) 

as suggested at split now complains about missing "isSeparator", I get the error message, "Missing argument for parameter 'isSeparator' in call."

If I jump to the definition of split, I find

func split<S : Sliceable, R : BooleanType>(elements: S, maxSplit: Int = default, allowEmptySlices: Bool = default, #isSeparator: @noescape (S.Generator.Element) -> R) -> [S.SubSlice]

I don't understand what the type of the last parameter is, which is perhaps the root of my problem. Can you tell me how I should call split, and even better can you explain what the parameter type is? Why isn't the type simply (S)->R? I am getting the line from a generator that reads a file line-by-line, if that makes any difference.

for line:String in reader! {
 let inputs = split(line) {$0 = " "}
 ...
} 
Community
  • 1
  • 1
saulspatz
  • 5,011
  • 5
  • 36
  • 47

1 Answers1

2

As said in the comments to the question, the correct way is to use the == operator instead of =.

The type (S.Generator.Element) -> R) must be interpreted in the light of the definition of split:

func split<S : Sliceable, R : BooleanType>
  (elements: S, 
   maxSplit: Int = default, 
   allowEmptySlices: Bool = default, 
   #isSeparator: @noescape (S.Generator.Element) -> R) 
      -> [S.SubSlice]

The type of split is a generic one: in other words, it is a function that can take as first parameter any value that satisfy a generic type (or protocol) subtype of Sliceable, like String, and return a result which must be a subtype of BooleanType (for instance true or false, which are instances of Bool). So the last parameter is a function which gets as parameter a type which is Element of Generator of S (for instance Character) and returns a value of type R. And {$0 == " "} is exactly a predicate of this type, that has an (implicit) parameter ($0), and check if it is equal to the character " ".

Renzo
  • 26,848
  • 5
  • 49
  • 61
  • A very lucid explanation, thank you. I was thinking of "generator" in the python sense. Can you please point me to where I can read more about the technicalities? – saulspatz Jul 03 '15 at 23:32
  • There are many introductory [resources](https://developer.apple.com/swift/resources/) on the Apple web site, for instance the video on [Advanced Swift](https://developer.apple.com/videos/wwdc/2014/?id=404). The advanced type system of Swift, however, is based on type technologies that were studied in the middle of '80s. One of the best technical papers, which is readable with a little effort, is the seminal [On Understanding Types, Data Abstraction, and Polymorphism](http://lucacardelli.name/Papers/OnUnderstanding.pdf) by L. Cardelli and P. Wegner. – Renzo Jul 04 '15 at 06:43