目标C代表声明 [英] Objective C Delegate declaration

查看:117
本文介绍了目标C代表声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我

  @property(nonatomic,weak)id delegate之间的区别; 

@property(nonatomic,weak)id< protocol_name>代表;

@property(nonatomic,weak)UIViewController *< protocol_name>代表;


解决方案


@property(nonatomic,weak)id delegate;


这指定当前类的对象有一个可以是任何类型的委托。代表对象的 weak 说明符是常见的,因为代表的对象不会增加委托人的引用计数(在ARC中,保持强有力的参考) 。 weak 委托参考是标准做法。


@属性(非原子,弱)id < protocol_name>委托;


这指出当前类的对象有一个可以是任何类型的委托id),但必须符合 protocol_name 协议。这特别有用,因为包含委托的类知道有可以发送给其委托的特定消息,并且知道代理将对其进行响应。


@property(nonatomic,weak)UIViewController *< protocol_name>委托;


这与第二个例子是一样的,除了委托必须是类的对象 UIViewController 。实际上, delegate 对象通常是 id 的类型,尽管这不是一个要求 - 它只是提供更大的自由度程序员






编辑:协议



假设你声明一个类如下:

  @interface MyObject:NSObject&MyDelegateProtocol> 
// ...
@end

< MyDelegateProtocol> 在此声明中意味着 MyObject 实现在 MyDelegateProtocol 协议(即符合协议)。



协议定义(类定义之前,显然)可能如下所示:

  @protocol MyDelegateProtocol< NSObject> 
@required
- (void)method1;
- (void)method2;
@optional
- (void)method3;
@end

这意味着任何对象符合 MyDelegateProtocol 协议必须实现名为的方法 - (void)method1 - (void) method2 。而且,可选可能包括消息 - (void)method3 的实现。



这是委托对象非常有用的信息(协议名称可以是任何方式,我只需要包含委托一词,使其显然被用作委托协议)。



如果另一个类现在定义了:

  @property(nonatomic,weak)id< MyDelegateProtocol>代表; 

知道可以依靠 -method1 -method2 由其代理执行,而 -method3 可以实施(可以使用如下代码检查):

  if ([self.delegate respondToSelector:@selector(method3)]){
[self.delegate method3];
}
else {
//委托不执行-method3。
}

支票不需要 -method1 -method2 ,因为这些方法是通过协议定义 @required ,它可以每次调用它们它需要。



一个类也可以一次使用多个协议(例如< Proto1,Proto2,UITableViewDelegate> ) - 有关协议的更全面的概述,请查看 Apple Docs协议


Can someone tell me the difference between

@property (nonatomic, weak) id delegate;

@property (nonatomic, weak) id  <protocol_name> delegate;

@property (nonatomic, weak) UIViewController * <protocol_name> delegate;

解决方案

@property (nonatomic, weak) id delegate;

This specifies that objects of the current class have a delegate that can be of any type. The weak specifier is common for delegate objects as it means the object with the delegate does not increment the delegate's reference count (in ARC-speak "keep a strong reference of it"). A weak delegate reference is standard practice.

@property (nonatomic, weak) id < protocol_name > delegate;

This specifics that objects of the current class have a delegate that can be of any type (id) but must conform to the protocol_name protocol. This is particularly useful as the class containing the delegate knows that there are specific messages that it can send to its delegate and "know" that the delegate will respond to them.

@property (nonatomic, weak) UIViewController * < protocol_name > delegate;

This is the same as the second example except that the delegate must be an object of class UIViewController. In practice, delegate objects are usually of type id, though this is not a requirement - it just offers greater freedom to the programmer.


EDIT: Protocols

Let's say you declare a class as follows:

@interface MyObject : NSObject <MyDelegateProtocol>
// ...
@end

The <MyDelegateProtocol> in this declaration means that MyObject implements the methods defined in the MyDelegateProtocol protocol (i.e. 'conforms to the protocol').

The protocol definition (previous to the class definition, obviously) may look like this:

@protocol MyDelegateProtocol <NSObject>
@required
- (void)method1;
- (void)method2;
@optional
- (void)method3;
@end

This means that any object 'conforming' to the MyDelegateProtocol protocol must implement methods called -(void)method1 and -(void)method2. And, optionally, may include an implementation for the message -(void)method3.

This is extremely useful information for delegate objects (the protocol name could be anything by the way, I just include the word 'delegate' to make it obvious that it is used as a delegate protocol).

If another class now defines:

@property (nonatomic, weak) id<MyDelegateProtocol> delegate;

the class knows that it can rely on implementations of -method1 and -method2 to be implemented by its delegate, and -method3 may be implemented as well (which it can check with code such as the following:)

if ([self.delegate respondsToSelector:@selector(method3)]) {
    [self.delegate method3];
}
else {
    // Delegate doesn't implement -method3.
}

The check is unnecessary for -method1 and -method2 as these methods are @required by the protocol definition, and it can call them whenever it wants.

A class can also use more than one protocol at a time (e.g. <Proto1, Proto2, UITableViewDelegate>) - for a more complete overview of Protocols, check out the Apple Docs on protocols.

这篇关于目标C代表声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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