这些关于iOS中的委托是否正确?是或否?

3

我长期以来一直在苦苦钻研委托,这是一个对我来说非常难的概念。我感觉现在我的知识更丰富了,但我距离自信还有很远的路要走。

如果有任何错误或不完整的地方,请告诉我原因。谢谢。

  1. Delegates implement protocols (unimplemented method headers)

  2. An object (a delegator) can register a delegate (that implements a protocol). This can be done by declaring a property of type id that implements a protocol:

    @property (weak, nonatomic) id <MyProtocol> myDelegate;
    
  3. The delegator can call certain methods (as specified in MyProtocol protocol) on the delegate

  4. Any class that implements the protocol and has to be Delegator's delegate, can declare itself as such:

    MyDelegator* myDelegator = segue.destinationViewController;
    myDelegator .delegate = self;
    
  5. Advantages of using a delegate:

    a. Reduces coupling (Delegate and Delegator are no longer dependent on each other) which is an important OO design principle

    b. makes Delegator more generic; it can now work with other objects, not just this Delegate

2个回答

3

1,2 - 是的

3 - 可以调用MyProtocol的任何方法,

4 - 正确。

5 - 几乎正确,可以在这里找到一个非常好的解释 delegate and controllers


1
  1. 是的。但协议可以是非正式的。
  2. 是的。
  3. 是的。
  4. 是和否。同样,代理可以非正式地实现方法...但是然后你会想要委托者在调用它之前验证代理是否respondsTo(或实现)了一个方法。

    代理不一定需要将自己设置为委托者的代理(事实上,它不应该这样做)。类比:一个商业专家来到我的公司,告诉我的老板将行政文书工作委派给他的秘书。 秘书(代理)没有告诉老板(委托者)要给它工作...是第三方这样做的。

    代理不必注册自己作为代理(再次强调,不应该这样做)。 它只需要能够完成工作。 通常代理不知道委托者的任何信息(没有指针)。 因此,代理不会有MyDelegator* myDelegator = segue.destinationViewController;这样的代码。

  5. A) 是的!

    B) 是的。本质上是“减少耦合”;

    添加C):它还允许您通过简单地在运行时更改委托来动态更改行为。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接