Skip to content

Instantly share code, notes, and snippets.

@perlguy99
Last active March 5, 2020 17:51
Show Gist options
  • Save perlguy99/828f97fb065054c5206e971cda0541a1 to your computer and use it in GitHub Desktop.
Save perlguy99/828f97fb065054c5206e971cda0541a1 to your computer and use it in GitHub Desktop.
My Favorite Swift Tips & Tricks & Articles

My Favorite Swift Tips & Tricks & Articles

iOS Dev Directory

iOS Dev Directory

Static factory methods in Swift

  • Great info on crating test stubs, extensions, etc.
  • Shows how to clearly separate setup code from actual logic.
  • Shows great alternatives to subclassing

Using Child View Controllers as plugins in Swift

  • What I love about using child view controllers is the fact that they get access to the exact same events as their parent view controller (things like viewDidLoad, viewWillAppear, etc), without having to be a subclass of it. They can also be responsible for their own internal layout, and perform their own controller logic. This enables us to structure our code very much like a suite of modular plugins, that can be added and removed as needed
  • Includes an extension on UIViewController that makes adding and removing Child View Controllers easy
  • Shows how to easily add an Error View Controller too

Dependency Injection using Factories in Swift

  • Instead of having objects either create their own dependencies or access them as singletons, it's the idea that everything an object needs in order to do its work should be passed in from the outside. This both makes it easier to see what exact dependencies a given object has, and it also makes testing a lot simpler - since dependencies can be mocked in order to capture and verify state & values.

Dependency Injection using Functions

  • When I'm only using a single function from a dependency, I love to inject that function as a closure, instead of having to create a protocol and inject the whole object. Makes dependency injection and testing super simple

Using Lazy Properties in Swift

Different Flavors of Dependency Injection in Swift

  • Initializer-based
    • Injection when initialized
  • Property-based
    • When inheriting from system classes
    • Especially when using XIBs or Storyboards
  • Parameter-based
    • Particularly useful when you want to easily make legacy code more testable
    1. Without having to change too much of its existing structure
    • Or, when you want to test static APIs

Core Data with SwiftUI

Core Data with SwiftUI

Sign in with Apple

Sign in With Apple Sign in With Apple 2

SwiftUI on the Mac

SwiftUI on the Mac Extras

Logging

Better Logging with Emoji

Misc

Convert a Set to an Array

    public var instanceArray: [Instance] {
        let set = instances as? Set<Instance> ?? []
        return set.sorted {
            $0.dateTime! < $1.dateTime!
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment