Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Closures Closures and Closure Expressions More Closure Shorthand

Benjamin Cooper
Benjamin Cooper
3,162 Points

Why does the challenge not work in actual 2.0 swift?

For some reason the filter function won't work when the array is written as [1...50]. I was wondering why that is. It will work if I write the actual numbers out within the array. Can someone tell me how I would have to write out the code, so I can use the filter function when the array is written as [1...50]. Thanks!

Ben

3 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

To be honest, I am not sure if you ever could initialize an int array like this [1...50], being it Swift 1 or 2. What this is rather doing is creating an array of ranges with one single range in it.

// This
let rangeArray = [1...10]
// is inferred to
let rangeArray: [Range<Int>] = [1...10]
// and thus can be used as
let rangeArray = [1...10, 1...5, 1...2]

You can, however, achieve what you want by initializing your array this way:

let intArray = [Int](1...10)  // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Hope that helps :)

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

From what I've read and hear, MUCH of what was in Swift is now deprecated in Swift 2.0. I'm not completely certain as I've just recently started Swift 2.0, but I have read that basically everything learned in Swift will not be applicable in Swift 2.0.

:)

Carson Carbery
Carson Carbery
9,876 Points

The pre Swift2.0 filter functionality has been depricated, so its not possible to answer this question using Swift 2.0 playground code. Instead if should be written :

let oddNumbers = numbers.filter(isOdd)