---
title: "#170: Swift Tricks Revisited 🎩"
url: https://littlebitesofcocoa.com/170-swift-tricks-revisited
author: Jake Marsh
published: 2016-01-15
topics: ["Swift","Techniques"]
description: "Today we'll look at a few more interesting Swift tips and tricks. Let's get started. We'll start with a simple one."
---

# #170: Swift Tricks Revisited 🎩

By Jake Marsh, January 15, 2016. HTML: https://littlebitesofcocoa.com/170-swift-tricks-revisited

Today we'll look at a [few](https://littlebitesofcocoa.com/164) [more](https://littlebitesofcocoa.com/159) interesting _Swift_ tips and tricks. Let's get started.

## Multiple Switch Cases

We'll start with a simple one. We can simplify messy switch statements by combining the similar cases. This is similar to how we can use multiple case statements in other languages, but a bit less wordy:

``` swift
extension FirstOrderAPI {
  var method: HTTPMethod {
    switch self {
    case .Spaceships,
         .Crew,
         .Stormtroopers,
         .Planets: return .GET
      
    case .AddSpaceship,
         .AddCrewMember,
         .AddStormtrooper,
         .AddPlanet: return .POST

    case .RemoveSpaceship,
         .RemoveCrewMember,
         .RemoveStormtrooper,
         .DestroyPlanet: return .DELETE
    }
  }
}

```

## Operator Parameters

We can actually pass literal **operators** in as a **parameter**, for example:

``` swift
[1, 2, 3, 4].reduce(0, combine: +)
```

This works because the `+` **operator** is actually a **function** under the hood:

``` swift
public func +(lhs: Int, rhs: Int) -> Int
```

## Simpler Reuse Identifiers

In **Objective-C**, It's quite common to use `NSStringFromClass` to use a **cell's** **class name** as it's `reuseIdentifier`. In _Swift_ though:

``` swift
let reuseIdentifier = String(SpaceshipTableViewCell)
```

Shout out to [Natasha](https://www.natashatherobot.com/nsstringfromclass-in-swift/) for pointing this one out!
