2

Task is simple: I need to create wrapper that delegates everything to wrapped object. Only the type of wrapped object is a protocol that contains optional methods. My wrapper is to implement the same protocol.

What do I do about optional methods? I can:

  1. I can implement them in my wrapper. But if they are not implemented by the object that gets wrapped what do I return from this implementations?
  2. Not implement them. In that case the wrapper object will not provide some of functionality of the wrapped object which I don't want.

Are there any better options? Please note that I am working in Swift.

4

1 回答 1

0

如果您使用可选方法,您将使用 Objective-C 运行时。这意味着您可以查询对象是否响应可选方法,这里有更好的解释。

如果您是 Objective-C 运行时的新手,您可以在Objective-C 运行时编程指南中阅读更多相关信息。

我认为包装器应该在运行时检查实现,如果缺少,那么应该在调用这些可选方法之一时返回某种指示符,可能是一个布尔值。

于 2015-04-04T19:57:56.147 回答