Access String value in enum without using rawValue

While I didn’t find a way to do this using the desired syntax with enums, this is possible using structs. struct DatabaseKeys { struct User { static let identifier = “User” static let Username = “username” } } To use: myUser[DatabaseKeys.User.Username] = “Johnny” Apple uses structs like this for storyboard and row type identifiers in … Read more

Swift 2.0 : ‘enumerate’ is unavailable: call the ‘enumerate()’ method on the sequence

Many global functions have been replaced by protocol extension methods, a new feature of Swift 2, so enumerate() is now an extension method for SequenceType: extension SequenceType { func enumerate() -> EnumerateSequence<Self> } and used as let mySwiftStringArray = [ “foo”, “bar” ] for (index, string) in mySwiftStringArray.enumerate() { print(string) } And String does no … Read more

Whats the Swift animate WithDuration syntax?

Swift 3/4 Syntax Here’s an update with the Swift 3 Syntax: UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: { self.username.center.x += self.view.bounds.width }, completion: nil) If you need to add a completion handler just add a closure like so: UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: { // animation stuff }, … Read more

Return instancetype in Swift

Similar as in Using ‘self’ in class extension functions in Swift, you can define a generic helper method which infers the type of self from the calling context: extension UIViewController { class func instantiateFromStoryboard(storyboardName: String, storyboardId: String) -> Self { return instantiateFromStoryboardHelper(storyboardName, storyboardId: storyboardId) } private class func instantiateFromStoryboardHelper<T>(storyboardName: String, storyboardId: String) -> T { … Read more

What is Protocol Oriented Programming in Swift? What added value does it bring?

Preface: POP and OOP are not mutually exclusive. They’re design paradigms that are greatly related. The primary aspect of POP over OOP is that is prefers composition over inheritance. There are several benefits to this. In large inheritance hierarchies, the ancestor classes tend to contain most of the (generalized) functionality, with the leaf subclasses making … Read more