Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Old / Incorrect Swift Syntax #5

Open
getaaron opened this issue Aug 24, 2014 · 1 comment
Open

Old / Incorrect Swift Syntax #5

getaaron opened this issue Aug 24, 2014 · 1 comment

Comments

@getaaron
Copy link

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
Copy link

let movie = object as? Movie);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants