Build Status MulticastDelegateSwift

MulticastDelegate

一个用 Swift 编写的优雅的多播委托,带有单元测试

安装

手动

MulticastDelegate.swift 复制到你的项目中

CocoaPods

	pod 'MulticastDelegateSwift'

Swift Package Manager

你可以使用 Swift Package Manager 并在 Package.swift 中指定依赖,添加如下内容

.Package(url: "https://github.com/jonasman/MulticastDelegate.git", majorVersion: 2)

用法

导入模块

	import MulticastDelegateSwift
  1. 添加到你的类: let multicastDelegate = MulticastDelegate<MyProtocol>()
  2. 其他类必须添加为委托: service.delegate.addDelegate(self)
  3. 当你需要通知你的委托时: multicastDelegate.invokeDelegates { delegate in delegate.done() }

替代版本

  1. 添加到你的类: let multicastDelegate = MulticastDelegate<MyProtocol>()
  2. 其他类必须添加为委托: service.delegate += self
  3. 当你需要通知你的委托时: multicastDelegate |> { $0.done() }

示例

protocol ServiceDelegate {
	func serviceGotData()
}

class Service {
	
	var delegate = MulticastDelegate<ServiceDelegate>()
	
	func fetchData() -> Bool {
		// fetch Data and notify your delegates
		// Call your delegates 
		delegate |> { delegate in
			delegate.serviceGotData()
		}
		return true
	}
}
class MainViewController: UIViewController, ServiceDelegate {
	
	func serviceGotData() {
	    	// do nothing
	}
}
let service = Service()
let viewController = MainViewController()
		
service.delegate += viewController
		
service.fetchData()

操作符

有 3 个操作符来简化多播的使用

+= 调用 addDelegate(delegate: T)

-= 调用 removeDelegate(delegate: T)

|> 调用 invokeDelegates(invocation: (T) -> ())

注意事项

代码基于使用弱对象的 HashTable,所以你不需要担心委托被释放

许可

MIT 许可证 (MIT)

版权所有 (c) 2014 João Nunes

特此授予任何人免费获得本软件及其相关文档文件(“软件”)的副本的权利,以处理本软件,不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售本软件的副本的权利,并允许向其提供本软件的人员这样做,但须符合以下条件

上述版权声明和本许可声明应包含在本软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何形式的明示或暗示保证,包括但不限于适销性、特定用途适用性和不侵权的保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任承担责任,无论是在合同、侵权或其他方面,因本软件或本软件的使用或其他交易而产生、发生或与之相关。