美文网首页
学习swift/Bond

学习swift/Bond

作者: 学习之路 | 来源:发表于2016-11-01 21:59 被阅读119次

github网址:https://github.com/ReactiveKit/Bond
参考链接:
http://www.osant.cn/2016/01/17/easyios-swift学习:初尝bond/
http://www.jianshu.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)

相关文章

网友评论

      本文标题:学习swift/Bond

      本文链接:https://www.haomeiwen.com/subject/ymtvuttx.html