Programming in Swift - Part 34: Part 4: Collections: | Ray Wenderlich

Learn how you can use closures to sort collections, filter collections, run calculations on elements within a collection, and more.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3535-programming-in-swift/lessons/34

var prices = [1.50, 10.00, 4.99, 2.30, 8.5]

let largePrices = prices.filter( { prices -> Bool in
return prices > 3
})

print(largePrices)

print statement is this [10.0, 4.9900000000000002, 8.5]

why does it get that when thats not even in the array?

@lac_ekim The print statement formats the array’s elements before printing them out in this case. If you want to get the exact value for each and every one of them, use a for in loop instead:

for price in largePrices {
  print(price)
}

Please let me know if you have any other questions or issues. Thank you! :]

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.