How to provide default Implementation of an Object

2019-07-21 00:34发布

问题:

In my code, I'm referring a protocol written in Objective-C in another protocol written in Swift like this:

// Objective-C
@protocol C
- (void)c;
@end

//Swift    
protocol A {
  var a : UIView { get }
}

extension A where Self: C {
  var a : UIView {
    return UIView()
  }

  func c() {
    // default implementation of c
    // self.a bla bla bla
  }
}
// Compiler will complain ViewController does not conform to C
class ViewController: UIViewController, A, C {
  //...
}

The problem is even though I provided default implementation of C in the protocol A, the ViewController seems unable to get it and the code won't compile due to the ViewController does not conform to C error. It would be fine if the protocol C is written in Swift.

In my actual project, the protocol C is from third party (GIDSignInDelegate) which I cannot simply rewrite them. It's a common usage if you want to build some abstraction upon other abstractions written in Objective-C.

I created a sample project here: https://github.com/linktoming/Protocol-Demo