The transition to Swift has gotten mixed reactions from the team. It is not easy to get started with a new programming language, but since Apple continues to maintain a close parity between the Swift APIs and their Objective-C APIs, it has been even harder to break old habits.

Observing the team’s frustrations with Swift, I was starting to realize that the biggest mistake we were making was writing Swift the Objective-C way. We are simply re-writing the same methods we’ve grown to love in Objective-C with a newer syntax rather than taking advantage of the powerful new features that are unique to swift.

The Swift way I admit, I’m still not entirely comfortable with optionals. Partially because it is new to me and partially because Xcode has at times been inconsistent with it’s compiler warnings when it comes to optionals.

However, one of the things I’m starting to like are the enhancements made to Switch statements in Swift.

// Swift the ObjC way

func fizzBuzz(num: Int) -> String {
  if num % 3 == 0 && num % 5 == 0 {
    return "FizzBuzz!"
  } else if num % 3 == 0 {
    return "Fizz!"
  } else if num % 5 == 0 {
    return "Buzz!"
  } else {
    return "\(num)"
  }
}

// Swift the Swift way

func fizzBuzz(num: Int) -> String {
  switch (num % 3 == 0, num % 5 == 0) {
  case (true, false):
    return "Fizz!"
  case (false, true):
    return "Buzz!"
  case (true, true):
    return "FizzBuzz!"
  default:  
    return String(num)
  }
}

One way isn’t any better than the other, and it may not make a big difference for something trivial, but now we have new ways to structure our code when faced with something more complex.



Follow my thoughts, checkout projects that interest me, connect with me professionally or send me an old fashioned email.