leverich / swiftislikescala

Swift is a lot like scala
MIT License
95 stars 25 forks source link

Old / Incorrect Swift Syntax #5

Open getaaron opened 10 years ago

getaaron commented 10 years ago

I really enjoyed reading your article. Here are a few minor changes based on newer versions of Swift.

Ranges

The half-open range operator .. is now ..<.

Empty Collections

Typed arrays are no longer declared and created using String[](); use [String]() instead.

Sort

Your Sort syntax is invalid:

sort([1, 5, 3, 12, 2]) { $0 > $1 }
  1. sort takes an array as inout parameter, which you aren't passing here
  2. you are passing in an immutable array, but the array needs to be mutable
  3. you can just pass the comparison operator.

So this would work:

var numbers = [1, 5, 3, 12, 2]
sort(&numbers)

Or this:

var numbers = [1, 5, 3, 12, 2]
numbers.sort(<)

Or you can return the sorted numbers using sorted instead of sort:

let immutableNumbers = [1, 5, 3, 12, 2]
let immutableSortedNumbers = [1, 5, 3, 12, 2].sorted(<)

Downcasting

This is more of a suggestion than a correction. You might want to cover optional downcasting (let movie = object as? Movie); I don't know if there's a Scala equivalent (I don't know Scala).

Droseyge92 commented 5 years ago

let movie = object as? Movie);