6

I want an extension for two classes UITextField and UITextView and the code is identical, but I have trouble coming up with an extension that will work for them both.

I am using ReactiveCocoa and I currently have this

import UIKit
import ReactiveCocoa
import enum Result.NoError

typealias NoError = Result.NoError

// How to DRY up this code?
extension UITextField {
  func textSignalProducer() -> SignalProducer<String, NoError> {
    return self.rac_textSignal()
      .toSignalProducer()
      .map { $0 as! String }
      .flatMapError { error in SignalProducer<String, NoError>(value:  "") }
  }
}

extension UITextView {
  func textSignalProducer() -> SignalProducer<String, NoError> {
    return self.rac_textSignal()
      .toSignalProducer()
      .map { $0 as! String }
      .flatMapError { error in SignalProducer<String, NoError>(value:  "") }
  }
}

How would I write an extension that would work for both? I was trying to do something like

protocol TextSignalProducer {}

extension TextSignalProducer where Self: ???? {
  // Same code as is duplicated in both current extensions...
}

but I have no idea how to specify Self as either UITextField or UITextView. Something like where Self == UITextField || Self == UITextView would probably make this possible.

Is there a nice way to accomplish what I want to try? Is this really necessary (I don't know the naming conventions for protocols/extensions)

import UIKit
import ReactiveCocoa
import enum Result.NoError

typealias NoError = Result.NoError

protocol TextSignal {
  func rac_textSignal() -> RACSignal!
}

extension UITextField: TextSignal, TextSignalProducer {}
extension UITextView: TextSignal, TextSignalProducer {}

protocol TextSignalProducer {}

extension TextSignalProducer where Self: TextSignal {
  func textSignalProducer() -> SignalProducer<String, NoError> {
    return self.rac_textSignal()
      .toSignalProducer()
      .map { $0 as! String }
      .flatMapError { error in SignalProducer<String, NoError>(value:  "") }
  }
}

I am using Swift 2.1, Xcode 7.2 and ReactiveCocoa 4.0.1

Filuren
  • 661
  • 2
  • 7
  • 19

2 Answers2

1

You can cut down your proposed solution to a single dummy protocol:

protocol TextSignalProducer {
    func rac_textSignal() -> RACSignal!
}

extension TextSignalProducer {
    func textSignalProducer() -> SignalProducer<String, NoError> {
        return self.rac_textSignal()
            .toSignalProducer()
            .map { $0 as! String }
            .flatMapError { error in SignalProducer<String, NoError>(value:  "") }
    }
}

extension UITextField: TextSignalProducer {}
extension UITextView: TextSignalProducer {}

I don't think there's a more concise way than that, though. UITextField and UITextView's rac_textSignal() implementations have nothing in common.

Ian Henry
  • 22,255
  • 4
  • 50
  • 61
0

UITextView and UITextField conform to UITextInput protocol. If rac_textSignal base on this protocol (I'm not sure because I don't have any project with RactiveCocoa at hand :) ) you can do this:

protocol Cos {
    func textSignalProducer() -> String
}

extension UITextView: Cos {

}

extension UITextField: Cos {

}

extension Cos where Self: UITextInput {
    func textSignalProducer() -> String {
        return "dsfsdf"
    }
}

let cos = UITextView()
cos.textSignalProducer()

let cos2 = UITextField()
cos2.textSignalProducer()
Tomasz Pikć
  • 753
  • 5
  • 21
  • Good idea with `UITextInput`, unfortunately it does not work. I think they are implemented one by one, here is for `UITextField` https://github.com/ReactiveCocoa/ReactiveCocoa/blob/master/ReactiveCocoa/Objective-C/UITextField%2BRACSignalSupport.h I'm not sure what `RACSignalSupport` means here, it is of undeclared type if I try to use that as `Self: RACSignalSupport` – Filuren Feb 15 '16 at 23:58