好例子网vip_使用示例清洁Swift VIP

本文翻译自Medium上的文章,深入探讨了Clean Swift VIP架构,并提供了一个清晰的示例,帮助开发者理解如何在Swift项目中应用这一架构。
摘要由CSDN通过智能技术生成

好例子网vip

干净的Swift VIP (Clean Swift VIP)

Clean Swift (a.k.a VIP) is Uncle Bob’s Clean Architecture applied to iOS and Mac projects. The Clean Swift Architecture is not a framework. It is a set of Xcode templates to generate the Clean Architecture components for you. That means you have the freedom to modify the templates to suit your needs.

Clean Swift(又名VIP)是Bob叔叔的Clean Architecture,适用于iOS和Mac项目。 Clean Swift体系结构不是框架。 它是一组Xcode模板,可以为您生成Clean Architecture组件。 这意味着您可以自由修改模板以适合您的需求。

  • View Controller

    查看控制器
  • Models

    楷模
  • Router

    路由器
  • Worker

    工人
  • Interactor

    互动者
  • Presenter

    主持人

查看控制器 (View Controller)

View Controller starts and ends the VIP cycle sends data to the Interactor though it doesn’t get responses from the Interactor This class has a one-sided interaction with the Presenter View Controller gets responses from the Presenter but can’t transfer anything to it

View Controller开始和结束VIP周期将数据发送到Interactor,尽管它没有从Interactor获得响应此类与Presenter进行了单方面交互View Controller从Presenter获得了响应,但无法将任何内容传递给它

import UIKitprotocol IMostPopularViewController: class {
var router: IMostPopularRouter? { get set }
}class MostPopularViewController: UIViewController {
var interactor: IMostPopularInteractor?
var router: IMostPopularRouter? override func viewDidLoad() {
super.viewDidLoad()
// do someting...
}
}extension MostPopularViewController: IMostPopularViewController {
// do someting...
}

楷模 (Models)

The Models class is related to each component in the VIP model Models is a class containing such structures as Request, Response, and ViewModel:

Models类与VIP模型中的每个组件相关。Models是一个类,其中包含诸如Request,Response和ViewModel的结构:

  • Request

    请求

    A request model contains parameters sent to the API request, which are user inputs such as text entered in text fields and values chosen in pickers.

    请求模型包含发送到API请求的参数,这些参数是用户输入,例如在文本字段中输入的文本和在选择器中选择的值。

  • Response

    响应

    This type of model receives the response from the API and stores the appropriate data.

    这种类型的模型从API接收响应并存储适当的数据。

  • ViewModel

    视图模型

    This model encapsulates responses sent to the Presenter in primitive data types such as String and Int.

    该模型将发送到Presenter的响应封装为原始数据类型,例如String和Int。

import UIKit
struct MostPopularModel {
struct Request {
// do someting... func parameters() -> [String: Any]? {
// do someting...
return nil
}
} struct Response {
// do someting...
}
}

路由器 (Router)

The Router deals with transitions by passing data between view controllers this done with helper classes

路由器通过在视图控制器之间传递数据(通过帮助程序类完成)来处理过渡

action occure in view controller call Router class with delegate method

视图控制器调用带有代理方法的路由器类中发生操作

//MARK: - in View Controller 
func getMostPopularArticles(){
router?.navigateToArticle(article_id : id )
}
//MARK: - in Router
import UIKit
protocol IMostPopularRouter: class {
func navigateToArticle(article_id : Int )
}class MostPopularRouter: IMostPopularRouter {
weak var view: MostPopularViewController?

init(view: MostPopularViewController?) {
self.view = view
}

func navigateToArticle(article_id : Int ){
view?.navigate(type: .modal, module: GeneralRoute.mostPopularArticles, completion: nil)
}
}
  • General Root

    一般根

    contain all modules routing from view controller and passing data trhough general root select you destination pass parameters to destination configration class

    包含所有从视图控制器路由并通过通用根目录传递数据的模块,选择目标传递参数至目标配置类

import Foundation
import UIKitenum MostPopularRoute: IRouter {
/*
If you want passing with parameters
you just add like this:
*/
case sample
case sample2(parameter: [String: Any])
/*
you can use: String, Int, [String: Any], etc..
*/
}extension MostPopularRoute {
var module: UIViewController? {
/*
Setup module with parameters like:
*/
switch self {
case .sample:
return SampleConfiguration.setup()
case .sample2(let parameters):
return SampleConfiguration2.setup(parameters: parameters)
}

}
}
  • Configuration

    组态

    The Configurator is a class that initializes all the Clean Swift components. Yet this class is optional. You can configure scenes by creating a private function in ViewController.

    Configurator是用于初始化所有Clean Swift组件的类。 但是此类是可选的。 您可以通过在ViewController中创建私有功能来配置场景。

import Foundation
import UIKitclass MostPopularConfiguration {
static func setup(parameters: [String: Any] = [:]) -> UIViewController {
let controller = MostPopularViewController()
let router = MostPopularRouter(view: controller)
let presenter = MostPopularPresenter(view: controller)
let manager = MostPopularManager()
let interactor = MostPopularInteractor(presenter: presenter, manager: manager)

controller.interactor = interactor
controller.router = router
interactor.parameters = parameters
return controller
}
}

工人 (Worker)

The Worker handles all the API and Core Data requests and responses and prepares data for the Interactor It also sends success and error responses to the Interactor

Worker处理所有API和核心数据请求和响应,并为Interactor准备数据。它还将成功和错误响应发送给Interactor

import Foundationprotocol IArticlesManager: class {
func mostPopularFromApi(complition: @escaping (NSError?, Bool, ArticlesModel.MostPopularArticle?) -> Void)
}class ArticlesManager: IArticlesManager {
func mostPopularFromApi(complition: @escaping (NSError?, Bool, ArticlesModel.MostPopularArticle?) -> Void) {
NetworkService.share.request(endpoint: ArticlesEndpoint.mostPopular, success: { (responseData) in
let response = responseData
do {
let decoder = JSONDecoder()
let data = try decoder.decode(ArticlesModel.MostPopularArticle.self, from: response)
print(data)
complition(nil , true , data)
} catch let error {
print("error : ", error.localizedDescription)
complition(error as NSError , true , nil)
}
})
}
}

互动者 (Interactor)

The Interactor is an intermediary between the Worker and Presenter First, it communicates with the ViewController which passes all the query parameters required for the Worker Before sending data to the Worker the Interactor checks this data If everything is good, the Worker returns the response and the Interactor sends a response to the Presenter

Interactor是Worker和Presenter之间的中介,首先,它与ViewController通信,该ViewController传递了Worker所需的所有查询参数。在将数据发送给Worker之前,Interactor会检查该数据。如果一切正常,Worker返回响应,并且交互者将响应发送给演示者

//MARK: - in View Controller 
func getMostPopularArticles(){
interactor?.getMostPopularArticles()
}


//MARK: - in Interactorimport UIKitprotocol IArticlesInteractor: class {
var parameters: [String: Any]? { get set }
func getMostPopularArticles()
}class ArticlesInteractor: IArticlesInteractor {
var presenter: IArticlesPresenter?
var manager: IArticlesManager?
var parameters: [String: Any]? init(presenter: IArticlesPresenter, manager: IArticlesManager) {
self.presenter = presenter
self.manager = manager
}


func getMostPopularArticles() {
manager?.mostPopularFromApi( complition: { (error, success, response) in
if(success == true){
print("getMostPopularArticles Done.....")
self.presenter?.showResponse(response: response)
} else {
self.presenter?.showErrorAlert(title: "Error", msg: error?.localizedDescription )
}
})
}
}

主持人 (Presenter)

The Presenter is responsible for presentation logic. It decides how data will be presented to the user. The Presenter organizes the response sent by the Interactor into view models suitable for display. Next, the Presenter passes those view models back to the View Controller to display to the user

演示者负责演示逻辑。 它决定如何将数据呈现给用户。 演示者将交互器发送的响应组织到适合显示的视图模型中。 接下来,Presenter将这些视图模型传递回View Controller以显示给用户

import UIKitprotocol IArticlesPresenter: class {
func showErrorAlert(title: String, msg: String?)
func showResponse(response: ArticlesModel.MostPopularArticle?)
}class ArticlesPresenter: IArticlesPresenter {
weak var view: IArticlesViewController?

init(view: IArticlesViewController?) {
self.view = view
}
func showErrorAlert(title: String, msg: String?) {
view?.showErrorAlert(title: title, msg: msg)
}
func showResponse(response: ArticlesModel.MostPopularArticle?) {
view?.showResponse(response: response)
}
}

Clean Swift的优点 (Advantages of Clean Swift)

  • Ready-made templates

    现成的模板
  • Unidirectional flow of data

    单向数据流
  • Testability

    可测性
  • Reusability

    可重用性
  • Collaboration

    合作

Clean Swift的缺点 (Disadvantages of Clean Swift)

  • Barriers to entry

    进入壁垒
  • Over engineering

    过度工程

you can check the source code on GitHub from HereThank you :)

您可以从这里在GitHub上检查源代码谢谢:)

翻译自: https://medium.com/dev-genius/clean-swift-vip-with-example-6f6e643a1a01

好例子网vip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值