0

Hello there i am using the latest Xcode 7 beta 6 and appears to be a problem with the filter method of an array.

in the following example i am trying to reproduce this : search the array and if you find any numbers biggest than 40 throw them away and after you do all that return the array filtered. It used to work with Xcode 6.4 but now it doesn't what am i missing here? cause in the argument once i type filter and press enter i get this :

(includeElement: (Self.Generator.Element) throws -> Bool

this is the code for Xcode 6.4

var someIntArray2 = [1,34,56,92,12,32,124,23,1293]

someIntArray2.filter({$0 < $40})
1
  • Take the $ before the 40 out. (someIntArray2.filter{$0 < 40}) Commented Sep 4, 2015 at 3:21

1 Answer 1

2

To expand on my previous comment:

In someIntArray2.filter({$0 < $40}), the $40 is checking for the 40th argument passed to the closure and compares the 0th parameter to it, but the closure that the filter method expects only takes one argument.

The line should instead be converted to someIntArray2.filter({$0 < 40}) which will compare the passed argument to the number 40 as intended.


(Since the closure is the last argument of the function, it is also possible to remove the parentheses which would turn the line into someIntArray2.filter{$0 < 40})

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

1 Comment

yeah really thank you. i am a new beginner with swift myself and you just helped me completely understand closure mechanism. perhaps now you can explain me the different a trailing closure has except from the aesthetic?

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.