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
// This API is available in iOS 16, macOS 13.0, tvOS 16.0, watchOS 9.0. | |
// Using backDeployed we can make it available in old OSes, like iOS 15 etc | |
extension Font { | |
@backDeployed(before: iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0) | |
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | |
public static func system(size: CGFloat, weight: Font.Weight? = nil, design: Font.Design? = nil) -> Font { | |
fatalError("Implement") | |
} | |
} |
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
struct Circle { | |
let centre: CGPoint | |
let radius: Double | |
} | |
extension Circle { | |
init(diameter: Double) { | |
centre = .zero | |
radius = diameter / 2 | |
} | |
} |
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
extension RangeReplaceableCollection where Element: Equatable { | |
@discardableResult mutating func remove(_ element: Element) -> Bool { | |
guard let index = self.firstIndex(of: element) else { return false } | |
remove(at: index) | |
// Remove other duplicate elements by recursivly calling it again | |
remove(element) | |
return true | |
} | |
} |
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
let first = ["a": 1, "b": 2] | |
let second = ["a": 9, "c": 3] | |
// value in `first` wins if the same key in both | |
let merged1 = first.merging(second, uniquingKeysWith: { current, _ in current }) | |
// value in `second` wins if the same key in both | |
let merged2 = first.merging(second, uniquingKeysWith: { _, new in new }) | |
extension Dictionary { | |
func mergingUniqueKeys(from other: [Key: Value]) -> [Key: Value] { |
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
import UIKit | |
final class FittingHeightCollectionViewController: UICollectionViewController { | |
private let items: [[String]] | |
init(items: [[String]]) { | |
self.items = items | |
let layout = UICollectionViewFlowLayout() | |
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize | |
layout.itemSize = UICollectionViewFlowLayout.automaticSize |
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
extension ContentView { | |
final class ViewModel: ObservableObject { | |
@Published var selectedImage: UIImage? | |
@Published var isPresentingImagePicker = false | |
private(set) var sourceType: ImagePicker.SourceType = .camera | |
func choosePhoto() { | |
sourceType = .photoLibrary | |
isPresentingImagePicker = true | |
} |
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
struct ImagePicker: UIViewControllerRepresentable { | |
typealias UIViewControllerType = UIImagePickerController | |
typealias SourceType = UIImagePickerController.SourceType | |
let sourceType: SourceType | |
let completionHandler: (UIImage?) -> Void | |
func makeUIViewController(context: Context) -> UIImagePickerController { | |
let viewController = UIImagePickerController() | |
viewController.delegate = context.coordinator |
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
extension Publisher where Self.Failure == Never { | |
public func notifyObjectWillChange(_ objectWillChange: ObservableObjectPublisher) -> AnyCancellable { | |
return self.sink { _ in | |
objectWillChange.send() | |
} | |
} | |
} |
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
extension ContentView { | |
final class ViewModel: ObservableObject { | |
private let package: Package | |
private var cancellables = [AnyCancellable]() | |
init(package: Package) { | |
self.package = package | |
// Model -> View Model | |
package.recipient.publisher(for: \.firstName) |
NewerOlder