github網址:https://github.com/ReactiveKit/Bond
參考鏈接:
http://www.osant.cn/2016/01/17/easyios-swift學習:初嘗bond/
http://www.lxweimin.com/p/5a76247243fe
//labbel跟隨輸入框變換 三種方法
1、 _ = textField.bnd_text.observeNext { (text:String?) in
self.desLabel.text = text
print(text)
}
2、textField.bnd_text.bind(to: desLabel.bnd_text)
3、textField.bnd_text.bind(to: desLabel)
/*
科普閉包 map的使用
map方法,其獲取一個閉包表達式作為其唯一參數。 數組中的每一個元素調用一次該閉包函數,并返回該元素所映射的值(也可以是不同類型的值)。 具體的映射方式和返回值類型由閉包來指定。
當提供給數組閉包函數后,map方法將返回一個新的數組,數組中包含了與原數組一一對應的映射后的值。
來看看map的定義 func map(transform: (T) -> U) -> U[] ,這里 T 和 U 都是泛型 ,指一種類型 , T 和U 只兩個不同的類型 ,也可以相同 。
來看個例子 :
我們用一個Int類型數組存儲商品金額,想把每個金額前面添加一個字符“¥”
let prices = [10,20,30]
let strPrices = prices.map { "¥\($0)" }
這個語法大家應該不陌生吧 ,陌生的去把上節閉包重新看一遍因為map只有一個參數,后面直接用了閉包的尾隨 ,不會加括號了
得到的結果 :print(strPrices) //[¥10, ¥20, ¥30]
這只是一個簡單的實例 ,其實你可以對每個元素進行很復雜的運算,這里不再贅述 ,用法如此,點到為止 、哈哈
*/
//綁定添加前綴后綴都可
//注意此處閉包只能這樣寫
textField.bnd_text.map { "Hi\($0!)" }.bind(to: desLabel)
//按鈕綁定觸發事件
1、
_ = okButton.bnd_tap.observe { e in
dump(e)
print(e)
print("哈哈")
}
2、
_ = okButton.bnd_controlEvents(.touchUpInside).observeNext { e in
print("??")
}
3、
_ = okButton.bnd_controlEvents(.touchUpInside).observeNext {
print("??")
}
okButton.bnd_isEnabled 按鈕是否可點擊
*/
/*
//根據校驗規則 判定按鈕是否可點就
combineLatest(textField.bnd_text, passTextField.bnd_text) { (text, passText) ->Bool in
let bool:Bool = text!.characters.count > 0 && passText!.characters.count > 0
return bool
}.bind(to:okButton.bnd_isEnabled)