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 = " "}
 ...
} 
3
  • let inputs = split(line) {$0 == " "} Commented Jul 3, 2015 at 18:24
  • 2
    Comparison is done with ==. = is an assignment operator. Commented Jul 3, 2015 at 18:24
  • It's hard to believe I looked at the code so many times, but overlooked that. I feel so stupid, Thanks. Commented Jul 3, 2015 at 23:32

1 Answer 1

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 " ".

Sign up to request clarification or add additional context in comments.

2 Comments

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?
There are many introductory resources on the Apple web site, for instance the video on Advanced Swift. 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 by L. Cardelli and P. Wegner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.