简体   繁体   English

目标C代表声明

[英]Objective C Delegate declaration

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"). weak说明符对于委托对象是通用的,因为它意味着具有委托的对象不会增加委托的引用计数(在ARC中说“保留它的强引用”)。 A weak delegate reference is standard practice. weak代表参考是标准做法。

@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. 具体说明当前类的对象具有可以是任何类型(id)但必须符合protocol_name协议的委托。 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 . 这与第二个示例相同,只是委托必须是UIViewController类的对象。 In practice, delegate objects are usually of type id , though this is not a requirement - it just offers greater freedom to the programmer. 在实践中, delegate对象通常是id类型,虽然这不是一个要求 - 它只是为程序员提供了更大的自由。


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 (ie 'conforms to the protocol'). 此声明中的<MyDelegateProtocol>表示MyObject实现MyDelegateProtocol协议中定义的方法(即“符合协议”)。

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 . 这意味着任何“符合” MyDelegateProtocol协议的对象必须实现名为-(void)method1-(void)method2 And, optionally , may include an implementation for the message -(void)method3 . 并且, 可选地 ,可以包括消息的实现-(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:) 该类知道它可以依赖-method1-method2实现由其委托实现,并且-method3 可以实现(它可以使用以下代码检查:)

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. 检查对于-method1-method2是不必要的,因为这些方法是由协议定义@required ,它可以随时调用它们。

A class can also use more than one protocol at a time (eg <Proto1, Proto2, UITableViewDelegate> ) - for a more complete overview of Protocols, check out the Apple Docs on protocols . 一个类也可以一次使用多个协议(例如<Proto1, Proto2, UITableViewDelegate> ) - 有关协议的更完整概述,请查看Apple Docs on protocols

@property (nonatomic, weak) id delegate; @property(非原子,弱)id委托;

A property with no specific type or protocol implementation. 没有特定类型或协议实现的属性。 When calling methods on delegate , anything goes - the compiler will trust you if it can see that a method exists somewhere and the runtime will check if you were lying. delegate上调用方法时,任何事情都会发生 - 编译器会相信你,如果它可以看到某个方法存在,运行时会检查你是否在撒谎。

@property (nonatomic, weak) id < protocol_name > delegate; @property(非原子,弱)id <protocol_name>委托;

A property with no specific type, but which implements a specified protocol. 没有特定类型但实现指定协议的属性。 You can only call methods from that protocol (unless you do some casting). 您只能从该协议调用方法(除非您进行一些转换)。 Any instance that is set to the property must conform to the protocol (or again, you need some casting). 设置为属性的任何实例都必须符合协议(或者,您需要一些转换)。

@property (nonatomic, weak) UIViewController * < protocol_name > delegate; @property(非原子,弱)UIViewController * <protocol_name>委托;

A property with a specific type ( UIViewController ) and which implements a specified protocol. 具有特定类型( UIViewController )并实现指定协议的属性。 You can only call methods from that protocol and from the UIViewController class (unless you do some casting). 您只能从该协议和UIViewController类调用方法(除非您进行一些转换)。 Any instance that is set to the property must conform to the protocol and be a subclass of UIViewController (or again, you need some casting). 设置为属性的任何实例必须符合协议并且是UIViewController的子类(或者,您需要一些转换)。

In the first example: 在第一个例子中:

@property (nonatomic, weak) id delegate;

you create a property of type id which is 'any' type in objective c with name - delegate. 你创建了一个id类型的属性,它是带有name - delegate的objective c中的'any'类型。

The second example: 第二个例子:

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

you create a property of type id which needs to conform to protocol_name protocol with name - delegate. 你创建了一个id类型的属性,它需要符合protocol_name协议与name - delegate。

The last example: 最后一个例子:

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

you create a property of type UIViewController (pointer to UIViewController) which needs to conform to protocol_name protocol with name - delegate. 你创建了一个UIViewController类型的属性(指向UIViewController的指针),它需要使用name - delegate符合protocol_name协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM