Created
September 23, 2020 14:01
-
-
Save DenTelezhkin/ca98f057cd0c62c9a9f309274f3302a9 to your computer and use it in GitHub Desktop.
StateObject alternative on iOS 13 / macOS Catalina
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 SwiftUI | |
protocol ViewModelContainable: View { | |
associatedtype ViewModel : ObservableObject | |
init(model: ViewModel) | |
} | |
// This struct is a direct MVVM alternative to @StateObject in iOS 14 and Mac OS Big Sur. | |
struct StateWrapped<U: ViewModelContainable, T> : View | |
where U.ViewModel == T | |
{ | |
@State var model: T | |
var body : some View { | |
U(model: model) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you just want a propertyWrapper that simply replaces @StateObject: https://gist.github.com/Amzd/8f0d4d94fcbb6c9548e7cf0c1493eaff