Nº 4 - Using variadic parameters

Want your array parameters to look way cooler?

Boring

1
2
3
4
5
func trapInUpsideDown(people: [String]) {
// ... your code here
}
trapInUpsideDown(people: [“Dustin”,”Mike”,”Lucas”, "Will"])

Cooler look

1
2
3
4
5
func trapInUpsideDown(_ people: String...) {
// ... your code here
}
trapInUpsideDown(“Dustin”,”Mike”,”Lucas”, "Will")

😉

Compartir Comentarios

Nº 3 - Printing multiple variables at once?

Wanna print multiple variables?

Problem

1
2
3
4
5
let name = "Tony"
let lastName: "Stark"
print(name)
print(lastName)

Better solution

Wrap them on a tuple.

1
2
3
4
let name = "Tony"
let lastName: "Stark"
print((name, lastName))

Awh, syntactic sugar ☺️

Compartir Comentarios

Nº 2 - Ditch the semicolon

Every time you use a semicolon (;) in swift, a tree dies 🙄. Don’t roll your eyes! It’s true.

Don’t use them, they are a thing of the past. They make your code look ugly and nasty.

Look how ugly:

1
2
let lukeSkywalker = JediKnight();
lukeSkywalker.savePrincessLeia();

Now look how beautiful:

1
2
let lukeSkywalker = JediKnight()
lukeSkywalker.savePrincessLeia()

Nice, clean, perfect 😌.

Compartir Comentarios

Nº 1 - Closure retain cycle

Avoid calling self inside your closure as much as possible.

1
2
3
let tower = { [weak self] rapunzel in
return self?.knight.save(rapunzel)
}

What if self is nil? I know what you’re thinking. Forget unowned! Just pass a capture list with the properties you need inside the closure.

1
2
3
let tower = { [knight] rapunzel in
return knight.save(rapunzel)
}

And that is how it is done.

Compartir Comentarios