@Observable @Bindable: MVVM in iOS 17

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so we got a bunch of new stuff at dub.dc for Swift UI and in this video I want to just quickly go over how mvvm Works in iOS 17 and with the new Swift data stuff so first of all let's import Swift data for some reason these new Swift macros are part of Swift data not part of Swift UI I'm not sure why or if this is actually intended since I'm not using it with swift data right now in this video but you will see in a second what I mean with that so I just have a dummy view here that has a state variable of a text which is just an empty string and a text field that binds to that text so this is exactly what you would have in previous with UI versions as well now I want to introduce a view model to hold this variable here so I don't have the data in my view as you would do with muvm usually so let's create our view model which is a class as always but now we don't need to inherit from observable object instead we can just create our text variable here and just turn it into an empty string again but now the magic part is that we actually need to use a macro here which is at observable this observable macro turns all of the publicly facing so in this case internal and public variables in my view model basically into what ad published would have done earlier so now this is basically a published variable in the view model but I don't need to add publish to every single variable here as I would have had before so now just add the add observable macro before or at the Declaration of your view model everything else will get automatically generated by xcode which is what a macro does in Swift and now I want to actually use that in my view and for that I can just add the model and just initialize it right here so this will be a view model but right now I will not be able to bind to that view model to be able to bind to the variables inside of the view model I will have to add another macro here with or actually it's a property wrapper I believe which is called add bindable and now I will be able to say dollar model dot text here in my text field which allows me to bind to that text and let's also add a create text field Style just so we can actually see it in the preview and there it is just has a in there as that's the placeholder of a text field this is the basic concept you will create your view models as classes and annotate them with the add observable Swift macro and then in your views you will create your view models as always but instead of observed objects State objects and environment objects you now just use the add bindable property wrapper if you want to bind to these values if you only want to read them of course so if we get rid of the text field here and just have a text of model of text that we can of course get rid of the add bindable since now we don't want to create a binding to that text and if we enter something here again we can actually see it in the preview as well all right and as I said for some interesting reasons so let me reward uh revert all of this so we have our text field and now I add bindable again I'm not using any Swift data here right so should I be able to comment out Imports with data for some reason then it doesn't know what add bindable is so this is only part of Swift data not parallel Swift UI keep that in mind and you get this Arrow here that the initializers are unavailable you need to actually import Swift data in order to use the add binder property wrapper this was just a very quick example let me know in the comments down below if you have suggestions or any of the new topics that you would like to see me make a video on Cheers
Info
Channel: Flo writes Code
Views: 5,917
Rating: undefined out of 5
Keywords: swift, swiftui, combine, xcode, ios, app, development, programming, code, apple, academy, tutorial, build, iphone
Id: zK92IwrT-Ls
Channel Id: undefined
Length: 4min 14sec (254 seconds)
Published: Tue Jun 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.