扩展 ReactiveCocoa
Extending ReactiveCocoa
我正在使用 ReactiveCocoa 4.0.4 alpha 1 和 Swift 2.1。我正在尝试编写一个扩展,在 UITextField
上创建最大文本限制。
extension RACStream {
public func max(textField: UITextField, max: Int) -> RACStream! {
return filter { next in
if let str = next as? String {
let ret = str.characters.count < max
if !ret {
textField.text = str[0..<max-1]
}
return ret
}
return true
}
}
}
self.inputTextField.rac_textSignal()
.max(self.inputTextField, max: 7)
.throttle(0.25)
.subscribeNext { (obj: AnyObject!) -> Void in
let input = Int(obj as! String)
print(input)
}
我在尝试调用 max
时遇到错误。它告诉我 Value of type RACStream has no member throttle
。如果我在 throttle
之后调用 max
,它会给出类似的错误。
我查看了 RACStream
class 检查过滤器。作为 returns Self!
,它指的是 RACStream
,我假设当我扩展 class 时,通过 return 一个 RACStream!
会导致类似的行为。为什么我的管道的其余部分不响应我的扩展功能?
根据 source code 方法 throttle
,subscribeNext
是 class RACSignal
的成员(它是 [=14 的子 class =]),但您的扩展方法适用于 class RACStream
并且您调用 returns 类型 RACSignal
的方法 rac_textSignal()
。因此,为了消除问题中描述的错误,您应该为 RACSignal
而不是 RACStream
编写扩展名。
extension RACSignal {
public func max(textField: UITextField, max: Int) -> RACSignal {
// method filter can be invoked since RACSignal is subclass of RACStream
return filter { next in
if let str = next as? String {
let ret = str.characters.count < max
if !ret {
textField.text = str[0..<max-1]
}
return ret
}
return true
}
} // Func
} // RacSignal Ext
我正在使用 ReactiveCocoa 4.0.4 alpha 1 和 Swift 2.1。我正在尝试编写一个扩展,在 UITextField
上创建最大文本限制。
extension RACStream {
public func max(textField: UITextField, max: Int) -> RACStream! {
return filter { next in
if let str = next as? String {
let ret = str.characters.count < max
if !ret {
textField.text = str[0..<max-1]
}
return ret
}
return true
}
}
}
self.inputTextField.rac_textSignal()
.max(self.inputTextField, max: 7)
.throttle(0.25)
.subscribeNext { (obj: AnyObject!) -> Void in
let input = Int(obj as! String)
print(input)
}
我在尝试调用 max
时遇到错误。它告诉我 Value of type RACStream has no member throttle
。如果我在 throttle
之后调用 max
,它会给出类似的错误。
我查看了 RACStream
class 检查过滤器。作为 returns Self!
,它指的是 RACStream
,我假设当我扩展 class 时,通过 return 一个 RACStream!
会导致类似的行为。为什么我的管道的其余部分不响应我的扩展功能?
根据 source code 方法 throttle
,subscribeNext
是 class RACSignal
的成员(它是 [=14 的子 class =]),但您的扩展方法适用于 class RACStream
并且您调用 returns 类型 RACSignal
的方法 rac_textSignal()
。因此,为了消除问题中描述的错误,您应该为 RACSignal
而不是 RACStream
编写扩展名。
extension RACSignal {
public func max(textField: UITextField, max: Int) -> RACSignal {
// method filter can be invoked since RACSignal is subclass of RACStream
return filter { next in
if let str = next as? String {
let ret = str.characters.count < max
if !ret {
textField.text = str[0..<max-1]
}
return ret
}
return true
}
} // Func
} // RacSignal Ext