代理和通知之间有什么区别? [英] what is the difference between Delegate and Notification?

查看:178
本文介绍了代理和通知之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代理和通知有什么区别?

what is the difference between Delegate and Notification?

我理解为委托和协议,

@protocol classADelegate

-(void)DelegateMethod;


@end



classB <classADelegate>{

   classA *ObjOfclassA=[[classA alloc]init];

    ObjOfclassA.delegate=self;

//while push later, here we have taken the pointer of classB(self) to classA and stored in delegate variable of classA. so from classA we can call the function in classB

   push:classA from here.


   -(void)DelegateMethod{

        nslog(@"i am rithik from India");


     }

}


classA{

   id <classADelegate> delegate;

   -(void)viewdidload{

        [self.delegate DelegateMethod];

    }

}

1 为什么我们不在类A中使用

1 why dont we use in classA like this

classA{

**classB** <classADelegate> delegate;


[self.delegate DelegateMethod];

}

使用 / strong>和它们的区别是什么?

what is the reason for using "id" and what is the difference of them?

2 我们调用了ClassB的DelegateMethod函数的方法。

2 we have call the method of classB's DelegateMethod function which is came from protocol definition.

,我们可以通过定义classB的实例方法直接调用该方法。因为我们在classA的委托变量中得到了classB的指针。

instead we can straightly call that method by defining that instance method of classB.because we have the got the pointer for classB in classA's delegate variable.

这样。

classB{

-(void)DelegateMethod;

}

然后在

classA{

       classB delegate;

       -(void)viewdidload{

            [self.delegate DelegateMethod];

        }

    }






所以从上面的我们已经避免了协议和id变量

但我知道很多人使用代理和协议。这里我来了解任何优点,而使用委托和协议

but i knew many of us use delegate and protocol. here i came to know about any advantages while use delegate and protocol

这里是DelegateMethod函数的方法的协议实现的用法。

here what is the usage of protocol implementation for method of DelegateMethod function.

而不是实例定义。

@protocol的用法是什么。

what is the usage of those by @protocol.

请任何一个指导我正确的方向...

please any one guide me right direction...

我是新的iphone开发。

i am new to iphone developement.

我知道如何创建委托。但当我来研究NSNotification

right now i knew how to create delegate .but while i came to study about the NSNotification

,这也是做几乎正确的工作,如委托。

that is also do the almost right job like delegate.

所以我应该使用delgate或NSnotification。

so when should i use the delgate or NSnotification.

ThankU

推荐答案

简答:您可以像电话呼叫一样考虑代表。你打电话给你的朋友,特别想和他们谈谈。你可以说些什么,他们可以回应。你可以说话,直到你挂断电话。代理,以同样的方式,在两个对象之间创建一个链接,并且你不需要知道代理将是什么类型,它只需要实现协议。另一方面,NSNotifications就像一个广播电台。他们向任何愿意倾听的人广播他们的信息。无线电台不能从它的听众接收反馈(除非它有电话或代表)。侦听器可以忽略该消息,或者它们可以使用它做某事。 NSNotifications允许您向任何对象发送消息,但是您不会在它们之间有来回通信的链接。如果你需要这个通信,你应该实现一个委托。否则,NSNotifications更简单,更容易使用,但可能会让您陷入麻烦。

Short Answer: You can think of delegates like a telephone call. You call up your buddy and specifically want to talk to them. You can say something, and they can respond. You can talk until you hang up the phone. Delegates, in much the same way, create a link between two objects, and you don't need to know what type the delegate will be, it simply has to implement the protocol. On the other hand, NSNotifications are like a radio station. They broadcast their message to whoever is willing to listen. The radio station can't receive feedback from it's listeners (unless it has a telephone, or delegate). The listeners can ignore the message, or they can do something with it. NSNotifications allow you to send a message to any objects, but you won't have a link between them to communicate back and forth. If you need this communication, you should probably implement a delegate. Otherwise, NSNotifications are simpler and easier to use, but may get you into trouble.

长答案

代理通常是更适当的处理方式,特别是如果你正在创建一个框架供别人使用。当您与代理使用协议时,您可以获得编译时检查所需的方法,因此,如果缺少任何必需的方法,则知道编译时。对于NSNotificationCenter,您没有这样的保证。

Delegates are usually a more appropriate way of handling things, especially if you're creating a framework for others to use. You gain compile time checking for required methods when you use protocols with your delegates, so you know when you compile if you're missing any required methods. With NSNotificationCenter you have no such guarantee.

NSNotificationCenter是 hack-ish,经常使用新手程序导致糟糕的建筑。很多时候,这两个特性是可以互换的,但是更多的硬核开发人员可能会嘲笑使用NSNotificationCenter。

NSNotificationCenter is kind of "hack-ish" and is frequently used novice programmers which can lead to poor architecture. A lot of times these two features are interchangeable, but more "hardcore" developers might scoff at the use of the NSNotificationCenter.

Q: 使用id的原因是什么,以及它们的区别是什么?

strong> A:使用 id 可以将任何对象作为参数发送到方法。注意,你不能发送原语,如bools,float,double,ints等,除非它们被包装在各自的对象包装器中。

A: Using id allows you to send any object to the method as a parameter. Note that you can not send primitives such as bools, floats, doubles, ints, etc. unless they are wrapped in their respective Object wrappers.

classB{

-(void)DelegateMethod;

}

然后在

classA{

   classB delegate;

   -(void)viewdidload{

        [self.delegate DelegateMethod];

    }

}

你提供的将要求 classA 的委托总是类型 classB ,这是不利的。而不是在这种情况下使用委托,你可能只是使用一个引用你的其他类的变量,例如 myClassB 。代理的美妙之处在于你可以传递任何对象,代码只要能够实现所需的方法(编译器保证,只要它被标记为正确的代理类型)就可以工作。

The above example you provided would require that classA's delegate always be of type classB, which isn't advantageous. Instead of using delegates in this scenario, you would probably just use a variable that referred to your other class, say, myClassB. The beauty of delegates is that you can pass around any object, and the code just works as long as they implement the required methods (which the compiler ensures, as long as it's marked as the correct delegate type).

这篇关于代理和通知之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆