ReactiveCocoa

RAC类关系图:

ReactiveCocoa

RAC 信号源:

ReactiveCocoa

文本框监听

func racsTest() {
         textField.reactive.continuousTextValues.observeValues { (text) in
             
             print("值为:\(text ?? "")")
             
         }
     }

结果:

ReactiveCocoa

Map映射 用于将一个事件流的值操作后的结果产生一个新的事件流。

方法一:

textField.reactive.continuousTextValues.map { (text) -> Int in
            
            return (text?.count)!//!.characters.count
            
            }.observeValues { (count) in
                
                print("数值为:\(count)")
                
        }

输入: Hello123

结果为:

ReactiveCocoa

方法二:

1 let (signal, observer) = Signal<String, NoError>.pipe()
 2         signal.map { (string) -> Int in
 3             return string.lengthOfBytes(using: .utf8)
 4             }.observeValues { (length) in
 5                 print("长度length: \(length)")
 6         }
 7         
 8         observer.send(value: "123456")
 9         
10         observer.send(value: "some")

结果为:

ReactiveCocoa

filter函数可以按照之前预设的条件过滤掉不满足的值

相关推荐