<img src="https://raw.githubusercontent.com/yankodimitrov/SignalKit/SignalKit-4.0/Resources/logo.png" width="280" alt="SignalKit">
---
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
## Abstract
SignalKit is a lightweight event and binding framework. The core of SignalKit is the Observable protocol. Each implementation of the Observable protocol defines the type of the observation thus an Observable can sendNext only one type of event. For example an Observable of type String can only sendNext String values.
Another key protocol is SignalType which implements Observable and Disposable. Each SignalType implementation has a property <code>disposableSource: Disposable?</code> which points to a Disposable that comes before the current signal.
Because SignalType implements Disposable we can use the <code>disposableSource</code> property to chain signal operations like map, filter and combineLatest together. Each operation returns either a new SignalType or a Disposable. When we call <code>dispose()</code> on a SignalType it will dispose the whole chain of operations.
To store the chain of operations we can use a stored property or the <code>disposeWith(container: DisposableBag) -> Disposable</code> method on the Disposable protocol. When DisposableBag is deinitialized it will dispose all of its items for us.
```swift
let disposableBag = DisposableBag()
let userName = Signal<String>()
userName.next { print("name: \($0)") }
.disposeWith(disposableBag)
userName.sendNext("John") // prints "name: John"
```
![SignalKit Primary Protocols](https://raw.githubusercontent.com/yankodimitrov/SignalKit/SignalKit-4.0/Resources/primary-protocols.png)
## Events And Bindings
SignalKit comes with an elegant way to observe for different event sources like **KVO**, **Target Action** and **NSNotificationCenter** via an unified API by simply calling <code>observe()</code> method. The observe method is a protocol extension on the <code>NSObjectProtocol</code> which returns a SignalEvent with sender Self. Then we use Protocol Oriented Programming to add extensions to a SignalEventType protocol where Sender is from a given type.
![SignalKit Primary Protocols](https://raw.githubusercontent.com/yankodimitrov/SignalKit/SignalKit-4.0/Resources/event-model.png)
### Key Value Observing
Let's say we want to observe an instance of <code>class Person: NSObject</code> for it's <code>name</code> property changes with KVO. This is super easy with SignalKit, just call <code>observe()</code> on the instance and it will return the available events for this type. Then choose <code>keyPath(path: String, value: T)</code> where for the value parameter pass the initial value of the property. SignalKit will use this initial value type to perform an optional type cast on the values sent by KVO.
```swift
let person = Person(name: "John")
person.observe()
.keyPath("name", value: person.name)
.next { print("Hello \($0)") }
.disposeWith(disposableBag)
```
### Target Action
SignalKit comes with SignalEventType extensions for controls which inherits from <code>UIControl</code> and <code>UIBarButtonItem</code>:
```swift
let control = UIControl()
let barButton = UIBarButtonItem()
control.observe().events([.TouchUpInside])
.next { _ in print("Tap!") }
.disposeWith(disposableBag)
barButton.observe().tapEvent
.next { _ in print("Tap!") }
.disposeWith(disposableBag)
```
### NSNotificationCenter
SignalEventType also have a handy extensions for observing an instance of <code>NSNotificationCenter</code> for notifications:
```swift
let center = NSNotificationCenter.defaultCenter()
center.observe().notification(UIApplicationWillResignActiveNotification)
.next{ _ in print("Resign Active") }
.disposeWith(disposableBag)
```
### Bindings
Bindings in SignalKit are implemented again with protocol extensions. We extend the SignalType where the ObservationType (from the generic Observable protocol) is from a certain type and add method to bind the value to a UI control like <code>UILabel</code>.
Here is an example of binding the String value from the signal to the text property of UILabel:
```swift
let userName = Signal<String>()
let nameLabel = UILabel()
userName.bindTo(textIn: nameLabel)
.disposeWith(disposableBag)
```
## Signals
#### Signal
<code>Signal</code> is the primary object which you can use to send events and it is thread safe.
#### SignalValue
<code>SignalValue</code> is a signal that stores its last sent/current value. If we add a new observer to it it will send immediately its value to the newly added observer. If we change the value of the signal it will notify its observers for the change. <code>SignalValue</code> is also thread safe.
```swift
let name = SignalValue(value: "John")
name.next { print($0) }.disposeWith(bag) // prints "John"
name.value = "Jonathan" // prints "Jonathan"
```
#### CollectionEvent
We can use a signal of type <code>Signal<CollectionEvent></code> to send the changes that occur in our data source. Then we can bind the signal to <code>UITableView</code> or <code>UICollectionView</code> and the changes that we send will be reflected in the table/collection view:
```swift
// UsersListViewController
...
viewModel.usersChangeSignal.bindTo(tableView, rowAnimation: .Fade).disposeWith(bag)
// UsersListViewModel
let usersChangeSignal = Signal<CollectionEvent>()
var users = [User]()
...
var event = CollectionEvent()
users.insert(User(name: "John"), atIndex: 0)
event.itemInsertedAt(0, inSection: 0)
usersChangeSignal.sendNext(event)
```
## Operations
SignalKit comes with the following SignalType operations:
![SignalKit Primary Protocols](https://raw.githubusercontent.com/yankodimitrov/SignalKit/SignalKit-4.0/Resources/signal-operations.png)
## Extensions
Currently SignalKit comes with extensions for the the following <code>UIKit</code> components:
![SignalKit Primary Protocols](https://raw.githubusercontent.com/yankodimitrov/SignalKit/SignalKit-4.0/Resources/uikit-extensions.png)
### Keyboard
You can use the <code>Keyboard</code> structure to observe for keyboard events posted by the system. Then you will get back a structure of type <code>KeyboardState</code> which you can query for the keyboard end/start frame and other data that the system sends with the notification:
```swift
Keyboard.observe().willShow
.next { print($0.endFrame) }
.disposeWith(disposableBag)
```
## Installation
SignalKit requires Swift 2.0 and Xcode 7
#### Carthage
Add the following line to your [Cartfile](https://github.com/carthage/carthage)
```swift
github "yankodimitrov/SignalKit" "master"
```
#### CocoaPods
Add the following line to your [Podfile](https://guides.cocoapods.org/)
```swift
pod “SignalKit”
```
##License
SignalKit is released under the MIT license. See the LICENSE.txt file for more info.
没有合适的资源?快使用搜索试试~ 我知道了~
SignalKit是一个响应式Swift框架,专注于干净和可读的API。_Swift_下载.zip
共82个文件
swift:67个
png:5个
plist:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 86 浏览量
2023-04-26
11:23:09
上传
评论
收藏 186KB ZIP 举报
温馨提示
SignalKit是一个响应式Swift框架,专注于干净和可读的API。_Swift_下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
SignalKit是一个响应式Swift框架,专注于干净和可读的API。_Swift_下载.zip (82个子文件)
SignalKit-master
LICENSE.txt 1KB
SignalKit.podspec 940B
SignalKit
SignalKit.h 503B
SignalValue.swift 1KB
Protocols
Disposable.swift 491B
SignalEventType.swift 260B
Observable.swift 670B
SignalType.swift 6KB
Utilities
Bag.swift 1KB
DisposableAction.swift 471B
Scheduler.swift 2KB
CollectionEvent.swift 4KB
DisposableBag.swift 887B
ActionTarget.swift 681B
NotificationObserver.swift 1KB
KeyPathObserver.swift 1KB
ControlEventObserver.swift 1KB
Signal.swift 1KB
SignalEvent.swift 277B
Extensions
NSNotificationCenterExtensions.swift 792B
UIKit
Keyboard.swift 3KB
UIBarButtonItemExtensions.swift 732B
UIImageViewExtensions.swift 519B
UIBarItemExtensions.swift 532B
UITableViewExtensions.swift 4KB
UIControlExtensions.swift 1KB
UISwitchExtensions.swift 381B
UIViewExtensions.swift 821B
UICollectionViewExtensions.swift 3KB
UITextFieldExtensions.swift 405B
UIDatePickerExtensions.swift 396B
UISegmentedControlExtensions.swift 438B
UITextViewExtensions.swift 779B
UISliderExtensions.swift 393B
UILabelExtensions.swift 491B
NSObjectExtensions.swift 1KB
Info.plist 808B
SignalKit.xcodeproj
xcshareddata
xcschemes
SignalKit.xcscheme 4KB
project.pbxproj 48KB
Resources
event-model.png 53KB
uikit-extensions.png 30KB
logo.png 11KB
primary-protocols.png 20KB
signal-operations.png 25KB
SignalKitTests
SignalValueTests.swift 1KB
SignalTests.swift 8KB
Utilities
ActionTargetTests.swift 1KB
SchedulerTests.swift 4KB
DisposableActionTests.swift 820B
BagTests.swift 2KB
NotificationObserverTests.swift 2KB
ControlEventObserverTests.swift 2KB
DisposableBagTests.swift 1KB
CollectionEventTests.swift 7KB
KeyPathObserverTests.swift 2KB
SignalKitTests-Bridging-Header.h 103B
Fakes
MockCollectionView.swift 2KB
Person.swift 326B
MockControl.swift 2KB
MockTableView.swift 2KB
MockDisposable.swift 341B
Extensions
NSNotificationCenterExtensionsTests.swift 2KB
NSObjectExtensionsTests.swift 1KB
UIKit
UIViewExtensionsTests.swift 1KB
UISwitchExtensionsTests.swift 853B
UIBarButtonItemExtensionsTests.swift 790B
UITextViewExtensionsTests.swift 890B
UITextFieldExtensionsTests.swift 797B
KeyboardTests.swift 3KB
UIControlExtensionsTests.swift 2KB
UIDatePickerExtensionsTests.swift 863B
UICollectionViewExtensionsTests.swift 3KB
KeyboardStateTests.swift 4KB
UITableViewExtensionsTests.swift 4KB
UISegmentedControlExtensionsTests.swift 1KB
UILabelExtensionsTests.swift 746B
UIImageViewExtensionsTests.swift 807B
UIBarItemExtensionsTests.swift 816B
UISliderExtensionsTests.swift 830B
Info.plist 733B
.gitignore 180B
README.md 7KB
共 82 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- desc
- VB+ACCESS超市管理系统设计(源代码+系统).zip
- xiulian.jpg
- 130平米全包装修清单明细excel表格.xls
- TI杯大奖赛本科组+研究生组论文+源代码+PCB基于LM3S811的声控轮式机器人
- TI杯大奖赛本科组+研究生组论文+源代码+PCB基于GSM无线传输技术的远程手机遥控系统
- TI杯大奖赛本科组+研究生组论文+源代码+PCB基于FastICA盲源分离算法的语音增强系统
- 15第十五章常用医学统计软件简介.pdf
- TI杯大奖赛本科组+研究生组论文+源代码+PCB基于DSP和双向Z源逆变器的纯电动汽车电机驱动与车辆控制系统
- TI杯大奖赛本科组+研究生组论文+源代码+PCB基于DSP的机械手控制系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功