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
- Without having to change too much of its existing structure
- Or, when you want to test static APIs
Sign in With Apple Sign in With Apple 2
Convert a Set to an Array
public var instanceArray: [Instance] {
let set = instances as? Set<Instance> ?? []
return set.sorted {
$0.dateTime! < $1.dateTime!
}
}