NEVERCODE Develop apps 20% faster with the leading CI/CD partner for iOS, Android, Cordova, Ionic, React Native & Flutter projects Try It


#2: Chainable Methods πŸ”—


Making functions chainable is quite easy and can allow us to write using an almost DSL-like syntax.

We'll add a new function that does something and then return self. It's that simple.

enum CountdownType: Int { case ToTheSecond, ToTheDay }
enum ColorScheme: Int { case AfterMidnight, ClassyYellow, Tealfish }

class Concern {
  var title: String = ""
  func title(aTitle: String?) -> Concern {
    title = aTitle ?? ""; return self

  var subtitle = ""
  func subtitle(aSubtitle: String?) -> Concern {
    subtitle = aSubtitle ?? ""; return self

  var countdownType: CountdownType = .ToTheSecond
  func countdownType(type: CountdownType) -> Concern {
    countdownType = type; return self

  var colorScheme: ColorScheme = .AfterMidnight
  func colorScheme(scheme: ColorScheme) -> Concern {
    colorScheme = scheme; return self

It enables us to write extremely readable and composable code like this:

  .title("Big Meeting")
  .subtitle("With those people from that place")

Setting properties is just the tip of the iceberg here. Imagine using this technique to create chainable queries:

  .sort { $0.startDate < $1.startDate }

Chainables TLDR;

  • πŸ‘†write setters
  • πŸ–– return self
  • πŸŽ‰ profit