---
title: "#232: Practical Protocols 🔩"
url: https://littlebitesofcocoa.com/232-practical-protocols
author: Jake Marsh
published: 2016-05-03
topics: ["Swift","Techniques"]
description: "Swift Protocols are awesome. Understanding how they can (or should) fit into our code can be tricky. Today we'll take a baby step into the world of..."
---

# #232: Practical Protocols 🔩

By Jake Marsh, May 3, 2016. HTML: https://littlebitesofcocoa.com/232-practical-protocols

_Swift Protocols_ are awesome. Understanding how they can (or <i>should</i>) fit into our code can be tricky. Today we'll take a baby step into the world of protocols with a simple, but "real" example and try to illustrate the upsides. Let's get started.

We're going to be fancy and abstract away some of our layout code. So we'll create a little struct to hold some layout settings like this:

``` swift
struct LayoutSettings {
  let direction: FlexDirection
  let justification: Justification
  let alignmentSelf: Alignment
  let alignmentChildren: Alignment

  /// ...etc
}
```

See? Fancy. This is great if we want to specify each individual combination each time, but it'd be nice if we could define some sort of "pre-canned" layouts that we could use by name. Sounds like a great job for a **Swift Enum**.

``` swift
enum CannedLayout {
  case FillParent
  case SizeToFit
  case Absolute(point: CGPoint)
  case Relative(closure: (parentFrame: CGRect) -> CGSize)
}
```

Lovely, this will be handy. How are we going to wire all this together though? Simple, we'll make a _Protocol_ that's only responsibility is to convert itself to a **LayoutSettings**.

``` swift
protocol LayoutSettingsConvertible {
  func layoutSettings() -> LayoutSettings
}
```

**LayoutSettings** can adopt this extremely simply:

``` swift
extension LayoutSettings : LayoutSettingsConvertible {
  func layoutSettings() -> LayoutSettings { return self }
}
```

Whew! That was tough.

Making our **CannedLayout** **Enum** adopt our new _Protocol_ is a bit more involved, but really just means `switch`-ing over `self` and return the proper combination of settings for each case.

``` swift
extension CannedLayout : LayoutSettingsConvertible {
  func layoutSettings() -> LayoutSettings {
    switch self {
      case .FillParent: return LayoutSettings(direction: .Vertical, justification: .Start, alignmentSelf: .Stretch, alignmentChildren: .Start)
      /// ...etc
    }
  }
}
```

All that's left is to use this new protocol somewhere. Let's wire this up to UIView to make it useful:

``` swift
extension UIView {
  func layout(settings: LayoutSettingsConvertible) {
    /// configure the view for the new settings here
  }
}

```

Neat! Now, we can use configure views with one of our canned layouts:

``` swift
let v = UIView(frame: .zero)
v.layout(CannedLayout.FillParent)
```

But we can also easily configure them the "long way" using a full LayoutSettings object directly:

``` swift
let v = UIView(frame: .zero)
v.layout(LayoutSettings(direction: .Vertical, justification: .Start, alignmentSelf: .Stretch, alignmentChildren: .Start))
```

Now that we have this simple protocol, we can make other helper types like this:

``` swift
struct Row : LayoutSettingsConvertible {
  func layoutSettings() -> LayoutSettings {
    return LayoutSettings(direction: .Horizontal, justification: .Start, alignmentSelf: .Stretch, alignmentChildren: .Start)
  }
}
```

That's just the basics when it comes to _Protocols_. They have much more to offer. More on this topic soon!
