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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// KeyboardAwareModifier.swift | |
// KeyboardTest | |
// | |
import SwiftUI | |
import Combine | |
struct KeyboardAwareModifier: ViewModifier { | |
@State private var keyboardHeight: CGFloat = 0 |
If you look at a version like 1.5.3, then the 1 is considered the major number, the 5 is considered the minor number, and the 3 is considered the patch number.
If developers follow SemVer correctly, then they should:
- Change the patch number when fixing a bug as long as it doesn’t break any APIs or adds features.
- Change the minor number when they added features that don’t break any APIs.
- Change the major number when they do break APIs.
Thanks to Paul Hudson, Hacking with Swift
//
// ContentView.swift
// ResultTypeTest
//
// Created by Michalski (US), James B on 12/12/19.
// Copyright © 2019 Perlguy, Inc. All rights reserved.
Thanks to Paul Hudson Hacking with Swift
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
This won't work because the @State
property wrapper doesn't actually modify the value directly...
@State private var blurAmount: CGFloat = 0 {
didSet {
print("New value is \(blurAmount)")
From 100 Days of SwiftUI
To demonstrate property wrappers, I want to start with a simple struct that wraps some kind of BinaryInteger value. We’re going to give this thing some custom code when it comes to setting its wrapped value, so that if the new value is below 0 we instead make it exactly 0 so that this struct can never be negative.
Our code would look like this:
struct NonNegative<Value: BinaryInteger> {
var value: Value