[swift-users] Protocol Initializer Inheritance

Bradley Zeller bradley.r.zeller at gmail.com
Fri Nov 18 18:45:50 CST 2016


I am trying to construct an object that conforms to a protocol `A` and also sublclasses object `B`. Protocol `A` requires an initializer that will be used to construct it. Xcode will attempt to autocomplete the initializer but then ultimately gives the following error: 

`error: argument passed to call that takes no arguments`

Here is some sample code:

```
protocol MyProtocol {
  init(foo: Int, bar: String)
}

class MyOperation: Operation, MyProtocol {
  required init(foo: Int, bar: String) {
      super.init()
  }
}

func makeMyProto<T: MyProtocol>() -> T where T: Operation {
  return T(foo: 0, bar: "bar")
}

```

It’s worth noting that this only fails when subclassing `Operation`. Subclassing `NSObject` or several other objects seems to work. Hopefully there is a way to do this. Ultimately, I just want this generic method to construct a thing that is both an `Operation` and conforms to a protocol.

Any help here would be greatly appreciated. Thanks!
-Bradley


More information about the swift-users mailing list