关于ReactiveObjC和ReactiveSwift 系统通知在swift中的用法

注意观察forName属性,如果用swift 的语法UIResponder.keyboardWillShowNotification不会有任何反应       

NotificationCenter.default.rac_addObserver(forName: "UIKeyboardWillShowNotification", object: nil).subscribeNext { notifation  in

            print("监听到")

        }

底层调用

- (id <NSObject>)addObserverForName:(nullable NSNotificationName)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

那么用ReactiveSwift直接可调用swift的语法完美监听

NotificationCenter.default.reactive.notifications(forName: UIResponder.keyboardWillShowNotification, object: nil).observeResult { notifation in              

    print("监听到")        

        }

底层调用

open func addObserver(forName name: NSNotification.Name?, object obj: Any?, queue: OperationQueue?, using block: @escaping (Notification) -> Void) -> NSObjectProtocol

ReactiveSwift 是出自 ReactiveCocoa 小组的纯 Swift 风格 FRP。 ReactiveSwift 提供了可组合、可声明以及灵活的原语,建立在时间流的大概念之下。这些原语可用于统一展现通用 Cocoa 以及泛型编程模式,这些都是观察者的行为基础。例如委派模式、回调闭包、通知、控制动作、响应者链事件、Future/Promise 以及 K/V 的监控。所有的这些不同的机制都使用相同的方法进行呈现,可以很方便的将这些组合在一起,更少的意大利面条式的代码以及状态来弥补其的缝隙。示例代码:// Purchase from the vending machine with a specific option. vendingMachine.purchase     .apply(snackId)     .startWithResult { result         switch result {         case let .success(snack):             print("Snack: \(snack)")         case let .failure(error):             // Out of stock? Insufficient fund?             print("Transaction aborted: \(error)")         }     } // The vending machine. class VendingMachine {     let purchase: Action<Int, Snack, VendingMachineError>     let coins: MutableProperty<Int>     // The vending machine is connected with a sales recorder.     init(_ salesRecorder: SalesRecorder) {         coins = MutableProperty(0)         purchase = Action(state: coins, enabledIf: { $0 > 0 }) { coins, snackId in             return SignalProducer { observer, _ in                 // The sales magic happens here.                 // Fetch a snack based on its id             }         }         // The sales recorders are notified for any successful sales.         purchase.values.observeValues(salesRecorder.record)     } }编者注:函数响应式编程(Functional Reactive Programming:FRP)是一种和事件流有关的编程方式,其角度类似EventSoucing,关注导致状态值改变的行为事件,一系列事件组成了事件流。FRP是更加有效率地处理事件流,而无需显式去管理状态。具体来说,FRP包括两个核心观点:事件流,离散事件序列属性properties, 代表模型连续的值。一系列事件是导致属性值发生变化的原因。FRP非常类似于GOF的观察者模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值