I'm using ReactiveCocoa 4.0.4 alpha 1 and Swift 2.1. I'm trying to write an extension that creates a max text limit on a 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)
}
I get an error when I try to call max
. It tells me Value of type RACStream has no member throttle
. It gives a similar error if I call max
after throttle
.
I took a look at the RACStream
class inspect filter. Being it returns Self!
, which refers to RACStream
, I would assume that when I extend the class, by return an RACStream!
would result in similar behavior. Why won't the rest of my pipeline respond to my extended function?
According to source code methods throttle
, subscribeNext
are members of class RACSignal
(it's a subclass of RACStream
), but your extension method is intended for class RACStream
and you call the method rac_textSignal()
which returns type RACSignal
. Therefore, In order to eliminate an error described in your question you should write an extension for RACSignal
rather than 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