Swift: VIPER Design Pattern (Architecture, 2023, Xcode 14, Swift 5) - iOS Development

  Рет қаралды 65,677

iOS Academy

iOS Academy

3 жыл бұрын

In this video we will learn about the VIPER design/architecture pattern for Swift 5 and iOS. This is a common pattern for large apps to properly isolate objects and their responsibility.
👉🏼 Connect (personal LinkedIn) / afraz-siddiqui
🚀 Follow on LinkedIn / ios-academy
#swift #designpattern #ios
** Like my teaching style? Check out some of my most popular courses!
courses.iosacademy.io
Join this channel to get access to perks, code, groups, and more:
Building Instagram: courses.iosacademy.io/p/build...
Building TikTok: / @iosacademy
SwiftUI for Beginners: ios-academy.teachable.com/p/s...
Join the iOS Academy Community: iosacademy.io/
** Get Skillshare free for 2 Months and learn iOS
www.skillshare.com/r/user/afraz
** Manage all your investments from app earnings on Betterment!
bit.ly/3eBwlI9
** Grow your own KZfaq tech channel with TubeBuddy:
www.tubebuddy.com/iosacademy

Пікірлер: 135
@xiaoyuguo1370
@xiaoyuguo1370 2 жыл бұрын
Thanks for creating this video! It's pretty easy to understand. But I would like to point out that in your code, there are a few retain cycles with view, presenter, router and interactor. One option you can do is to make those protocols conforms to AnyObject, and mark these references as "weak": 1. router's ref to presenter 2. router's ref to view 3. presenter's ref to view 4. interactor's ref to presenter Eventually your object relationship will look like this in the memory, where -> means a strong reference router ^ | window -> View Controller -> Presenter -> Interactor Correct me if I'm wrong.
@sexypekk
@sexypekk 2 жыл бұрын
Although slightly dangerous, I think `unowned` would be acceptable also so we can assert that our objects that should exist do exist. The alternative would be a completely unresponsive app that people would have to kill and restart themselves anyway so I guess you pick your problems :)
@iOSInnovator
@iOSInnovator Жыл бұрын
I have the same question Interactor has a strong reference of the presenter and the presenter has strong reference of the interactor. It will create retain cycle.
@pifeeder9027
@pifeeder9027 Жыл бұрын
That tells a lot about the iOS academy
@rrahulc
@rrahulc Жыл бұрын
circular reference can be fixed by replacing one of the strong references with a weak or unowned reference.
@keenancookson8641
@keenancookson8641 29 күн бұрын
I started a new job and they use VIPER. I'd never used it before and I struggled to get through some explanations of VIPER (ADHD). This explanation was so concise and easy to understand. Thank you so much, man! ❤
@_arun_k
@_arun_k 2 жыл бұрын
I have been trying to learn this from a long time. Finally i found this hands on tutorial instead of those redundant concepts articles. Thank you
@JoffreyB
@JoffreyB 3 жыл бұрын
Finally! Was waiting for that
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Get pumped
@nelsonalva
@nelsonalva 3 жыл бұрын
I can't thank you enough. Keep the good work!
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Youre welcome
@tcleedev
@tcleedev 3 ай бұрын
Whenever I search for Swift/ iOS, I ended up with this channel :) I'll just search in this channel first next time lol
@junglee-bee
@junglee-bee 3 жыл бұрын
Pretty neat & clean. Clear thought process. I have basic understanding of VIPER & this video has been quite helpful. Thanks 🙏 - Request: Please make an extended version where there is a card view that appears from the bottom & can have a link that presents another screen. + like a reusable component that shows up from different screens, example from 2 different tabs. It wud be interesting to see VIPER tackle such a scenario. Basically its the Router part of VIPER that is fascinating me lately.
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Thanks
@lukasz.szarkowicz
@lukasz.szarkowicz 2 жыл бұрын
Good video explaining basics. I would like to see some more informations about coordination between modules. I use approach of adding Coordinator pattern into VIPER. Also, Router should be responsible only for routing not building a module - here is good to use some Module builder by using fabric pattern. This way we use SOLID principles. There is also questionable for me conforming to UITableViewDelegate and UITableViewDataSource in UIViewController component. What do you think about using Presenter to conform to that protocol - then we keep responsibility of proper presentation on the side of Presenter (view shouldn't convert DTO to ViewModel). I am also curious about automation for it. I created Xcode template which creates entire module with 1-click, but I am still researching for making it better - that's why I am looking for more opinions :) . Thank you for your work and I am waiting for part 2 with advanced part.
@maru027
@maru027 3 жыл бұрын
This is really helpful and clear for me to understand. Thank you!
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Youre welcome
@losinsky9356
@losinsky9356 3 жыл бұрын
You are doing very good content! At this time I'm exploring all your videos. Keep going 😁
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Thanks!
@Leopugpiano
@Leopugpiano 3 жыл бұрын
amazing job!
@BHFJohnny
@BHFJohnny 2 жыл бұрын
Couldn't manage to *destroy* that like button. It still works.
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Haha nice
@Charlot1914
@Charlot1914 2 жыл бұрын
They all connect by strong reference between each other? Where are the weak references?
@jawske8233
@jawske8233 3 жыл бұрын
Great video as always! Would you mind explaining how to design an app that will run on “small” devices such as iPhone 7 and on “bigger’s” such as iPhone 12 Pro Max? Thanks and keep up the good work
@mishar4743
@mishar4743 2 жыл бұрын
THANK U SO MUCH!!! IT REALY USEFUL!!! FROM RUSSIA WITH LOVE ❤️❤️❤️
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Youre welcome
@sandipkhedkar
@sandipkhedkar 2 жыл бұрын
And finally i understood the VIPER, thanks a lot man
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Youre welcome
@sashamakedonskii
@sashamakedonskii 3 жыл бұрын
it would be nice if you extend this tutorial for navigation on multiple screens, delegates, callbacks and how to handle this in VIPER appropriate. And yeah, thanks for video. Cleanest explanation of VIPER ever I see 👍
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Great suggestion!
@nurlanakylbekov7266
@nurlanakylbekov7266 3 жыл бұрын
Great content👍, I love when u start project without storyboard. VIPER is one of the design pattern that I want master but always fail. Hope with this tutorial I’ll get proper understanding.
@flutterswift7278
@flutterswift7278 3 жыл бұрын
Көп қолданылады ма VIPER Қазақстанда?
@nurlanakylbekov7266
@nurlanakylbekov7266 3 жыл бұрын
@@flutterswift7278 Банктерде колданылады
@sovrinfo
@sovrinfo 2 жыл бұрын
Great video! Thank you a lot!
@iOSAcademy
@iOSAcademy 2 жыл бұрын
You are welcome!
@abhishekthapliyal7933
@abhishekthapliyal7933 3 жыл бұрын
I was trying to learn VIPER but seems confusing, this video is so simple and easy to understand.
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Thanks!
@FilipeMarques
@FilipeMarques 3 жыл бұрын
Viper is a little bit confusing, but we can isolate the modules, functionalities, I think it was easier to test. Thank you so much for the great lesson! (:
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Youre welcome
@AndGordy
@AndGordy 2 жыл бұрын
A great intro to VIPER. Actually, go ahead and watch it!
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks
@tamasballa1
@tamasballa1 Жыл бұрын
Thanks, very nice video! My question is, wouldn't it be better to create an AnyEntity protocol, conform the User entity to it, and instead of the update with users method you could write a more generic update with data anyentity function?
@programming5523
@programming5523 Жыл бұрын
Great Explanation helps a lot . 🙌🏻
@iOSAcademy
@iOSAcademy Жыл бұрын
Glad to hear that!
@FernandoMXoficial
@FernandoMXoficial 3 жыл бұрын
Great content, very good explanation
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Thanks!
@miljanvukicevic8631
@miljanvukicevic8631 2 жыл бұрын
Awesome vid
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks
@barche75
@barche75 3 жыл бұрын
I’m don’t really like VIPER. To much classes. Most of classes knowing about other ones. View layer contacts with models. My preference to Data Driven MVVM + Coordinator (for routing) + Builder (factory to create units) + proxy pattern for fetching data from web or storage. Any way it’s a great video to understand this pattern. Thank you!
@iOSAcademy
@iOSAcademy 3 жыл бұрын
I agree
@glennadams7047
@glennadams7047 3 жыл бұрын
@@iOSAcademy Could you make an app for us demonstrating all of those ? It would be very valuable to see a "best practices" app with them rolled into one, and we could use it as a template for our future apps. Thanks for your great work !!!
@noodles7240
@noodles7240 3 жыл бұрын
Totally agree. Looks like the VIPER is an upgrade for MVP. However, MVP itself is not a perfect architecture due to the dependencies between the presenter and views. In the VIPER, it introduced a new dependency between interactor and presenter, which is a terrible thing. Meanwhile, the too many protocols make it hard to maintain if there's new business logic which requires a new parameter in a protocol, which needs to modify all the implementations of it. MVVM is a perfect architecture since it decouples the dependency of view and view model. The view model no longer needs to hold a reference of view. In Android, we can use live data or data binding to update the view, in iOS we can use RxSwift or things like that.
@barche75
@barche75 3 жыл бұрын
@@noodles7240 yes, that’s true. But I’m usually use closures to bind vm and view.
@shatadrudatta9405
@shatadrudatta9405 3 жыл бұрын
Excellent tutorial
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Thanks!
@alimehdi4011
@alimehdi4011 3 жыл бұрын
Hi! great learnings in your video and understood VIPER easily because of your video. I have a question that does your code produces retain cycles in router class where interactor, presenter, view contains each other references ?
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Thanks
@nashtravelandlifestyle
@nashtravelandlifestyle 11 ай бұрын
Thanks for the tutorial. I have question about the code written to create table, label etc. It is a function right, doesnt it get called everytime table or label is accessed and keeps creating new instance of table or label?
@MoAdel92
@MoAdel92 3 жыл бұрын
Thanks bro
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Youre welcome
@burakturhan9708
@burakturhan9708 Жыл бұрын
Hi do you have git for this? It would be better if also inspect on code to learn better
@user-in6bh9nd8k
@user-in6bh9nd8k 9 ай бұрын
22:38 “let” was pronounced very sensually :)) p.s. Thanks for lesson!
@abdullahumer1683
@abdullahumer1683 4 ай бұрын
What I do is that presenter will have reference to View, Interactor and router but only the View will have reference to the presenter. Interactor and Router does not need to have a reference of the Presenter. With using async and await the presenter will wait for interactor to do the network calls and return the data. Its also a good practice to have a separate helper class for the network calls instead of writing them completely in the interactor.
@antonilin3123
@antonilin3123 3 жыл бұрын
I think which pattern to use it's depends on project what you are working on. If it's small app then you can use only MVC. But, of course Viper, MVP and so on need to know :)
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Yep, i completely agree
@marcos6718
@marcos6718 2 жыл бұрын
I think maybe the pattern is a bit convoluted... I can’t see myself using it, to be honest. But great video, I learned a lot! Thank you a bunch!!
@lucassoares4069
@lucassoares4069 2 жыл бұрын
"Main storyboard file base name" doesnt show in my plist file but when I try to run the app it seems it is still there. Is there a chance it is hidden somehow?
@rev_kous9275
@rev_kous9275 3 жыл бұрын
Waiting for the Clean architecture implementation. Can be Clean VIPER, Clean MVVM,...
@SchoolBads
@SchoolBads 2 жыл бұрын
Can you send a link to the project please?
@dhav7634
@dhav7634 2 жыл бұрын
Thanks for the great video and explanation. Is there a github link to take a look at this sample project code?
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks - code available through iosacademy.io/resources
@rakhipatidar9373
@rakhipatidar9373 3 жыл бұрын
Please make a video using VIP architecture pattern for Swift 5
@dev_jeongdaeri
@dev_jeongdaeri 3 жыл бұрын
Oh my god😭👍👍👍
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Haha thanks
@TheAhmadReza
@TheAhmadReza Жыл бұрын
Thanks for this great tutorial, however, I still don't understand why entity (users) has to be in view! I think it would be better if it was in the interactor. also why we should have an API call in the router?! it also should be in the Interactor 🤔
@justinblackvfx
@justinblackvfx 3 жыл бұрын
I want to learn Viper but, I stuck how to implement uitabbarcontroller in viper. Can you show it how next time?
@lounge_shows
@lounge_shows 2 жыл бұрын
I understand that this is just for learning purposes, but I am guess there is a cyclic dependency that gets created here. The view has a reference to presenter and the presenter has a reference to view.
@BenjaminAnoh
@BenjaminAnoh 2 жыл бұрын
Thank you! VIPER has been so overrated. It is so clear now. Can you share the source code for this kind of tutorial?
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Youre welcome
@Ni7ram
@Ni7ram Жыл бұрын
hey, excellent video! and channel, to be honest. but i have a question: why is the router aware of the view? as i understand it, thats not compatible with the diagram of VIPER at the beginning of the video.. shouldnt it interact with it through the presenter? i think its more tedious, but cleaner.
@iOSAcademy
@iOSAcademy Жыл бұрын
Good call out. Ive seen both approaches
@edwardivash8539
@edwardivash8539 2 жыл бұрын
Great example! The only question left is why is the users array inside the view object? View doesn't need to know anything directly about the entity.
@IBGNW
@IBGNW Жыл бұрын
have you found the answer yet? I found another viper example also did the same.
@bonioctavianus308
@bonioctavianus308 8 ай бұрын
why does the interactor had to have a reference to presenter? doesn't it better if the interactor just return a result? hence it did not know anything about the caller = less coupled
@harshit2629
@harshit2629 7 ай бұрын
Thanks for video, where can I get the source code?
@asanlidev
@asanlidev 2 жыл бұрын
My most favourite part of the video: 02:49
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Haha thanks
@quoteu6240
@quoteu6240 3 жыл бұрын
Can we use viper and combine together ?
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Yes
@wlugick3326
@wlugick3326 3 жыл бұрын
What to to next? If I wanna tap the cell to get a network request and push to another Controller? Where and how I should write the codes?
@PauloSilva252
@PauloSilva252 2 жыл бұрын
You would use the router to move to the next screen
@Lhunath
@Lhunath 3 жыл бұрын
FYI- the "Any" prefix is intended to refer to a type erased opaque type that conforms to the protocol.
@iOSAcademy
@iOSAcademy 3 жыл бұрын
yep!
@RostyslavKobizsky
@RostyslavKobizsky 2 жыл бұрын
Lots of bad practices detected! But in general good introduction into the pattern itself
@kumarshastry9177
@kumarshastry9177 2 жыл бұрын
protocol is declared as AnyInteractor , AnyPresenter etc, but the methods like getUsers, didfetchusers inside protocols are confusing, i think they should be generic.protocols should not be concrete.
@chimansong8653
@chimansong8653 Жыл бұрын
It is got to solve the retain cycle.
@iOSAcademy
@iOSAcademy Жыл бұрын
Yep
@cinquain0
@cinquain0 2 жыл бұрын
Uuumm... I'll stick to MVVM. Thank you.
@GamePlay-sq8lf
@GamePlay-sq8lf 3 жыл бұрын
where to put validation code and how to pass data from one viewcontroller to other ?
@PauloSilva252
@PauloSilva252 2 жыл бұрын
You can use the router for that, since it is the router's responsibility to contain the logic for the flow of screens. This VIPER tutorial is good, but Viper is not really adequate for one screen apps, but rather for large apps with a lot of business logic in it. Validation code in what sense?
@gasparfreak
@gasparfreak 3 жыл бұрын
I don't know, maybe because I'm just a beginner I'm thinking it's a little too much setup, and really I can't get what are the benefits viper brings.
@iOSAcademy
@iOSAcademy 3 жыл бұрын
It is a lot but with huge projects it is a clean design
@gasparfreak
@gasparfreak 3 жыл бұрын
@@iOSAcademy By huge you mean, FANG level of hugeness correct, or am I missing something? Anything less than that I can use MVC or MVVM?
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Eh, its subjective. Like airbnb isnt fang but uses it. And othrs too
@PauloSilva252
@PauloSilva252 2 жыл бұрын
@@gasparfreak I work in a smaller company than FANG, ain't that big of an app and we use it too. It's good when you have a lot of business logic inside your app. If an app is heavily based on networking, I don't think Viper would be the best, MVC or MVVM is probably better
@harshitjain7428
@harshitjain7428 3 жыл бұрын
Is the source code for the same is available?
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Yes, for channel members
@roohbear
@roohbear 2 жыл бұрын
Wow, this is so fast! Did you practice this 3 or 4 times before recording this video? I'll have to watch it 2 or 3 times to fully inject it into my head but it seems to make sense. Personally, I think VIPER is overkill. Why take something simple and make it so complicated? I hate when things are over-engineered. It just seems like someone is trying to provide job security for themselves. There are no comments in this code. I would hate to have to try and figure this out. I would be cursing the developer and wondering "why, why, why" so many times. I seriously don't understand the point of it, but my new employer wants it probably because some manager heard it was new. Do you really need the "{ get set }" stuff? Near the end, when you were not getting any data showing up, I thought it might have been because you were missing that annoying "Allow arbitrary loads" setting in your info.plist file but for some reason, you didn't need that. Not sure why.
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Haha thanks - no, just experience
@rajdhakate617
@rajdhakate617 3 жыл бұрын
How you navigate so quickly without using touchpad?
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Command shift O
@ripfrauds
@ripfrauds 2 жыл бұрын
600 th like
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks!
@tibettenballs4962
@tibettenballs4962 2 жыл бұрын
2:48 'insert sponser'
@GrumpyOldTech
@GrumpyOldTech 2 жыл бұрын
Don’t think it is appropriate to make reference to User in the protocol definitions, should be generic to work across modules. Also an example of routing would be useful.
@alifayed5678
@alifayed5678 3 жыл бұрын
Can we have source code
@iOSAcademy
@iOSAcademy 3 жыл бұрын
Coming soon
@sexypekk
@sexypekk 2 жыл бұрын
Good overall I think, I skipped around a bit. I'm already familiar with VIPER and PRESIDIO (RIB architecture) and I've used both extensively but I like to get other perspectives on these things because I feel like there's always something I can do better. One major problem here though is that you added some specific use case functionality to your core Any* protocols. These functions should really be in separate protocols that extend the Any* protocols and then our concrete classes would implement those. For example `AnyPresenter` shouldn't be calling for user lists (unless all of your presenters would do that, which still seems like a fundamental flaw) but rather you'd have perhaps an `AnyUserPresenter`, then your `UserPresenter` would implement that protocol. It adds another layer of protocols which can be convoluted for an example but the good side of this is that you can now mock out these objects easily in test or swap implementations and do cool things with experimentation in production that would allow you, for example, to switch to a different type of UserView on the fly, with maybe a different UserPresenter that would give a different view into the same data more or less. I *think* you did this intentionally to water down the number of protocols and make the arch less overwhelming to pick up on, but good overview all around. nice work :)
@sexypekk
@sexypekk 2 жыл бұрын
one more thing, the `start()` method name I think is a bit misleading, but this looks more like a `bootstrap` or `build` factory method, I would expect `start` to be a type of lifecycle method that fires on `viewDidLoad` for example
@eaststone
@eaststone 2 жыл бұрын
I am a Korean developer studying viper. Thanks for the detailed explanation. Then, is it better to inherit the two protocols by describing common tasks in Any* protocol and user-related tasks in User* protocol?
@sexypekk
@sexypekk 2 жыл бұрын
@@eaststone that’s right! That’s what I would do. I would extract all common behavior for your Any* protocols and then make more specific use cases for each scenario like in a User* protocol. You can implement as many protocols as you want and you can separate them in swift using extensions and even put the extensions in separate files. With the caveat that extensions can’t have stored properties. So you’d have to declare those in the main class. This would probably only be used to keep your file sizes smaller once they get too large. Happy coding!
@eaststone
@eaststone 2 жыл бұрын
@@sexypekk Thank you!!! 👍
@eaststone
@eaststone 2 жыл бұрын
@@sexypekk Sorry, but I have one more question. I think protocols are meaningful for bundling repetitive code into blueprints. So, why implement a function for User as a protocol? This seems to be something used only in User *Class. Is there a big difference between implementing a function for User as a protocol versus implementing it inline?
@keepspeed
@keepspeed 2 жыл бұрын
If you have such a good pattern, use it yourself. When people understand patterns, they become the ability to make patterns. Do not deprive them of the opportunity to develop the ability to design optimal patterns by themselves trying to understand the patterns you have made. Any pattern is very boring and boring.
@iOSAcademy
@iOSAcademy 2 жыл бұрын
This isnt my pattern. Its common across industry
@bluer5640
@bluer5640 Жыл бұрын
Very fast-paced video and hard to understand. You got to the implementation so fast and confused me about where to put all these protocols and connections
@iOSAcademy
@iOSAcademy Жыл бұрын
Thanks for the feedback
@ziedagoubi7652
@ziedagoubi7652 6 ай бұрын
ok i was following until 21:25 then you lost me, you shouldn't be calling an api inside the initialiser
@abdullahumer1683
@abdullahumer1683 4 ай бұрын
yeah I thought the same. This can cause a race condition.
@1stplace362
@1stplace362 2 жыл бұрын
why don't you ever share the code source ughhh
@1987alejandroivan
@1987alejandroivan 6 ай бұрын
It would have been cool if the actual View/Interactor/Presenter actually take their references in the initializer, that way we wouldn't need to declare them as { get set } in the actual protocol, just { get }. protocol AnyView { var presenter: AnyPresenter { get } } class ViewController: UIViewController, AnyView { weak var presenter: AnyPresenter? // avoid retain cycle init(presenter: AnyPresenter) { self.presenter = presenter } } // etc.
@HarshitKumar-yn8du
@HarshitKumar-yn8du 7 ай бұрын
Thanks for video, where can I get the source code?
VIPER Architecture in iOS (Swift)
1:02:07
Venu Gopal Tewari
Рет қаралды 10 М.
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН
Mom's Unique Approach to Teaching Kids Hygiene #shorts
00:16
Fabiosa Stories
Рет қаралды 38 МЛН
What's going on with Windows Laptops?
10:30
Marques Brownlee
Рет қаралды 2,6 МЛН
When RESTful architecture isn't enough...
21:02
Dreams of Code
Рет қаралды 265 М.
Swift: WebSocket Real-Time Data Introduction (2022, iOS, Xcode 13)
16:08
Design Uber Eats - iOS System Design Interview
14:00
Andrey Tech
Рет қаралды 18 М.
Clean iOS Architecture pt.6: VIPER - Design Pattern or Architecture?
15:05
Essential Developer
Рет қаралды 37 М.
What are AI Agents?
12:29
IBM Technology
Рет қаралды 119 М.
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН