swift基础类swift实现cleanarchitecture用例,请参阅https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-arc(代

author author     2023-01-20     477

关键词:

/*
 The MIT License (MIT)
 
 Copyright (c) 2016 Nathan Smith
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/

import RxSwift

/**
 **Application Business Rule**
 
 Taken from [The Clean Architecture by Uncle Bob](https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html):
 
 The software in this layer contains application specific business rules. It encapsulates and implements all of the use cases of the system. These use cases orchestrate the flow of data to and from the entities, and direct those entities to use their enterprise wide business rules to achieve the goals of the use case.
 
 We do not expect changes in this layer to affect the entities. We also do not expect this layer to be affected by changes to externalities such as the database, the UI, or any of the common frameworks. This layer is isolated from such concerns.
 
 We do, however, expect that changes to the operation of the application will affect the use-cases and therefore the software in this layer. If the details of a use-case change, then some code in this layer will certainly be affected.
 
 - parameters:
 - Parameter: Type representing the input to the `UseCase`. If no input is required specify `Void`
 - Return: Type representing the ouput to the `UseCase`. If no output is required specify `Void`
 */
class UseCase<Parameter, Return> 
  /// Callers of `execute` are required to add its subscriber to this `disposeBag` e.g. `.addDisposableTo(useCase.disposeBag)`
  let disposeBag: DisposeBag
  
  private let observeOnScheduler: SchedulerType
  private let subscribeOnScheduler: SchedulerType
  
  /**
   - parameters:
   - disposeBag: Reference counted container for subscriptions, when this `UseCase` is deallocated all ongoing subscriptions will be cancelled
   - observeOnScheduler: Determines what thread observables are executed on. Typically this is a background thread pool e.g. `ConcurrentDispatchQueueScheduler(globalConcurrentQueueQOS: .Background)`
   - subscribeOnScheduler: Determines what thread subscribers are executed on. Typically this is the main thread e.g. `MainScheduler.instance`
   */
  init(disposeBag: DisposeBag, observeOnScheduler: SchedulerType, subscribeOnScheduler: SchedulerType) 
    self.disposeBag = disposeBag
    self.observeOnScheduler = observeOnScheduler
    self.subscribeOnScheduler = subscribeOnScheduler
  
  
  /**
   Abstract method that subclasses are required to implement. Unfortunately there is yet a method to tigger a compile error if missed.
   - parameters:
   - parameter: Input to the `UseCase`
   */
  internal func buildUseCaseObservable(parameter:Parameter) -> Observable<Return> 
    fatalError("Subclasses need to implement the `buildUseCaseObservable()` method.")
  
  
  /**
   **Examples:**
   ```
   let useCase = UseCase<Void, Void>(...)
   useCase.execute(()).subscribeNext() 
   // Do something as a result
   .addDisposableTo(useCase.disposeBag)
   ```
   ```
   let useCase = UseCase<Void, Int>(...)
   useCase.execute(()).subscribeNext()  count in
   print(count)
   .addDisposableTo(useCase.disposeBag)
   ```
   ```
   let useCase = UseCase<String, Int>(...)
   useCase.execute("Hello World").subscribeNext()  count in
   print(11)
   .addDisposableTo(useCase.disposeBag)
   ```
   
   - parameter parameter: Input to the `UseCase`. If the type `Parameter` is `Void` the value can be specified as `()`
   */
  func execute(parameter:Parameter) -> Observable<Return> 
    return buildUseCaseObservable(parameter)
      .observeOn(observeOnScheduler)
      .subscribeOn(subscribeOnScheduler)
  

将实现协议的 Swift 类导入到 Objective-C 类中

】将实现协议的Swift类导入到Objective-C类中【英文标题】:ImportingaSwiftclassthatimplementsaprotocolintoObjective-Cclass【发布时间】:2017-11-0723:36:21【问题描述】:我在Swift中有一个实现协议的类:classSwiftClass:SwiftProtocolfunctest()NSLog("Test");这... 查看详情

swift使用swift通用技术实现两个继承的路径类,其行为类似于objective-c中的动态合成。(代码片段)

查看详情

通过框架之间的扩展实现 Swift 协议一致性

...为一系列模块,主要以相当线性的方式依赖。这个问题的基础是:如果我在一个模块中有一个类,然后扩展它以符合另一个模块中的协议,那么该类型的所有对象都会自动符合吗?。 查看详情

为啥我在实现委托以将值从子 swift 类传递给父目标 C 类时出错?

】为啥我在实现委托以将值从子swift类传递给父目标C类时出错?【英文标题】:WhyamIgettingerrortoimplementdelegatetopassvaluefromachildswiftclasstoaparentobjectiveCclass?为什么我在实现委托以将值从子swift类传递给父目标C类时出错?【发布时间】... 查看详情

swift中多继承的实现(代码片段)

1.实现过程swift本身并不支持多继承,但我们可以根据已有的API去实现. swift中的类可以遵守多个协议,但是只可以继承一个类,而值类型(结构体和枚举)只能遵守单个或多个协议,不能做继承操作. 多继承的实现:协议的方法可以... 查看详情

swift学习笔记——基础语法

Swift学习笔记——基础语法本文是针对新手学习Swift笔记,记录学习过程的一些体会以及一些学习过程中的理解:基础语法第一次接触Swift或者Oc语法的时候,总感觉有点像脚本语言,不像Java或者C/C++。当然这... 查看详情

swift4基础

创建:2018/02/17  数据类型与变量                     控制类语句      & 查看详情

如何通过 Swift 扩展实现委托方法

】如何通过Swift扩展实现委托方法【英文标题】:HowdoIimplementdelegatemethodsthroughSwiftextension【发布时间】:2016-05-2507:46:33【问题描述】:我的项目是Obj-C和Swift的混合体,我正在尝试使用Swift扩展我的AppDelegate类。所以现在我有AppDeleg... 查看详情

在类中实现委托(iOS/Swift)

】在类中实现委托(iOS/Swift)【英文标题】:Implementingdelegateswithintheclass(iOS/Swift)【发布时间】:2018-05-0911:29:09【问题描述】:在移动开发中,接口和委托的实现如下(以定位服务为例):1.安卓、JavapublicclassmyClassimplementsLocationLi... 查看详情

swift与oc混编

...中的,我们只需要简单的配置,和一些注意细节,就可以实现Swift和OC的混编。步骤1:配置OC的桥接文件1、在Swift里第一次新建OC的文件,会出现如图:注意:以后再建,或者是拖入的OC文件,都 查看详情

swift学习(1基础语法)

Swift  基础语法基本1.取消了预处理命令2.取消了指针的概念3.取消了NS前缀4.大量将类替换成struct5.“;”在同一行用来分割语句,如果不是同一行可以省略6.可选项7.playground8.条件语句”if”,空执行”()”9.没有非0即真的逻... 查看详情

swift学习第十一天:类的定义

...介绍和定义Swift也是一门面向对象开发的语言面向对象的基础是类,类产生了对象在Swift中如何定义类呢?class是Swift中的关键字,用于定义类class类名:SuperClass{//定义属性和方法}注意:定义的类,可以没有父类.那么该类是rootClass通常情... 查看详情

swift-基础属性-属性写法

varnum1:Int=0varnum2:Int=5///1.计算属性varnum3:Intreturnnum1+num2///2.闭包属性privatelazyvarnum4:Int=returnnum1+num2()overridefuncviewDidLoad()super.viewDidLoad()print(num3)print(num4)   2.类属性class 查看详情

Swift 5 默认可解码实现,只有一个例外

】Swift5默认可解码实现,只有一个例外【英文标题】:Swift5DefaultDecododableimplementationwithonlyoneexception【发布时间】:2020-04-0916:04:06【问题描述】:有没有办法保持Swift对Decodable类的默认实现,只有Decodable对象但有一个例外?因此,... 查看详情

swift基础语法的学习(代码片段)

Swift基础语法的学习1.Swift中的模块化的标注;在OC中使用是:#pragmamark-标注一段的相关功能;在Swift中使用的是:MARK:-标注一段的相关功能;实现的效果如下图:2.Swift的基本“输入”,“输出”语句ÿ... 查看详情

React-native 桥接与 Swift 单例自定义类

...49【问题描述】:今天,我尝试在我的Javascript代码中使用实现单例模式的自定义Swift类。我已经创建了本机模块,但今天我遇到了这个错误:“致命错误:对类使用未实现的初始化程序\'init()\'”我猜是因为单例模式,但 查看详情

从 Swift 的 Objective-c 基础视图控制器继承

】从Swift的Objective-c基础视图控制器继承【英文标题】:InheritancefromanObjective-cbaseviewcontrollerfromSwift【发布时间】:2014-06-2013:17:53【问题描述】:我正在尝试将UIViewControllerObjective-C类迁移到Swift。此视图控制器继承自BaseViewController... 查看详情

在 Swift 中实现 copy()

】在Swift中实现copy()【英文标题】:Implementingcopy()inSwift【发布时间】:2014-06-1611:26:47【问题描述】:我希望能够在Swift中复制自定义类。到现在为止还挺好。在Objective-C中我只需要实现NSCopying协议,这意味着实现copyWithZone。例如... 查看详情