This file contains hidden or 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 Foundation | |
| @propertyWrapper struct Notifier<Value> { | |
| private let identifier: String | |
| var wrappedValue: Value? { | |
| didSet { | |
| NotificationCenter.default.post(name: Notification.Name(identifier), object: wrappedValue) | |
| } | |
| } |
This file contains hidden or 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 prints a list of buttons that on tap will fire a different type of haptic vibration | |
| import SwiftUI | |
| struct ContentView: View { | |
| let generator = UINotificationFeedbackGenerator() | |
| var body: some View { | |
| VStack(alignment: .center, spacing: 30.0) { | |
| Button(action: { |
This file contains hidden or 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 Binding { | |
| /// Wrapper to listen to didSet of Binding | |
| func didSet(_ didSet: @escaping ((newValue: Value, oldValue: Value)) -> Void) -> Binding<Value> { | |
| return .init(get: { self.wrappedValue }, set: { newValue in | |
| let oldValue = self.wrappedValue | |
| self.wrappedValue = newValue | |
| didSet((newValue, oldValue)) | |
| }) | |
| } | |
This file contains hidden or 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
| @DynamicColor(lightVariant: .black, darkVariant: .white) | |
| static var dynamicLabelColor: UIColor | |
| @propertyWrapper | |
| struct DynamicColor { | |
| let lightVariant: UIColor | |
| let darkVariant: UIColor | |
| var wrappedValue: UIColor { | |
| get { |
This file contains hidden or 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 Combine | |
| /** | |
| An observable, mutable property. | |
| Replays the current value when subscribed. | |
| */ | |
| @propertyWrapper | |
| struct Published<Output>: Publisher { | |
| typealias Failure = Never |
This file contains hidden or 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 Combine | |
| // When wrapping a value, we take advantage of the setter | |
| // to feed a PassthroughSubject | |
| @propertyWrapper | |
| struct Published<T> { | |
| private var innerValue: T | |
| private let innerSubject = PassthroughSubject<T, Never>() | |
This file contains hidden or 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 is a concept re-implementation of the @Binding and @State property wrappers from SwiftUI | |
| The only purpose of this code is to implement those wrappers myself | |
| just to understand how they work internally and why they are needed, | |
| ⚠️ This is not supposed to be a reference implementation nor cover all | |
| subtleties of the real Binding and State types. | |
| The only purpose of this playground is to show how re-implementing | |
| them myself has helped me understand the whole thing better |
This file contains hidden or 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 CoreData | |
| extension NSManagedObject { | |
| class var entityName: String! { | |
| get { | |
| return self.entity().managedObjectClassName.components(separatedBy: ["."]).last! | |
| } | |
| } | |