---
title: "#184: More Swift Tricks 🎩"
url: https://littlebitesofcocoa.com/184-more-swift-tricks
author: Jake Marsh
date: 2016-02-04
published: 2016-02-04
topics: ["Swift","Techniques"]
description: "Today we'll continue our look at Swift tips and tricks. Let's dive in. We'll begin with an organizational tip."
---

# #184: More Swift Tricks 🎩

By Jake Marsh, February 4, 2016. HTML: https://littlebitesofcocoa.com/184-more-swift-tricks

Today we'll [continue](http://littlebitesofcocoa.com/179-swift-tricks-properties) [our](http://littlebitesofcocoa.com/175-more-swift-tricks) [look](http://littlebitesofcocoa.com/170-swift-tricks-revisited) [at](http://littlebitesofcocoa.com/164-swift-tricks) _Swift_ tips and tricks. Let's dive in.

## Nested Types

We'll begin with an organizational tip. We can nest our type declarations inside one another to group related definitions:

``` swift
enum CostumeDepartment {
  case Day, Night

  struct Color { /* ... */ }
  struct Image { /* ... */ }
  struct Text { /* ... */ }
  
  func appLogoImage() -> Image { /* ... */ }
}
```

Swift's compiler is smart enough to take note of this nesting whenever ambiguity arises. This means we can use types nested inside multiple "container" types without any issues.

## Even-lazier Lazy Definitions

We covered the lazy keyword in [Bite #179](http://littlebitesofcocoa.com/179). It turns out, we can be <i>even lazier</i>, and omit the extra closure altogether. (Still works the same).

``` swift
lazy var backgroundView = SpecialBackgroundView()
```

## Variadic Parameters

**Objective-C** supported these as well, but they were a bit of pain to work with there. In _Swift_, we simply add an ellipsis (`...`) to our **function's** definition, and we receive the **arguments** as a strongly-typed _Swift_** array**.

``` swift
func sum(numbers: Int...) -> Int {  
  var total = 0
  for number in numbers { total += number }
  return total
}
```

...or if we wanted to write that **function** in an <i>even</i> <i>Swiftier</i> way:

``` swift
func sum(numbers: Int...) -> Int { return numbers.reduce(0, combine: +) }
```
