如何处理包含属性的Objective-C协议? [英] How to handle Objective-C protocols that contain properties?

查看:129
本文介绍了如何处理包含属性的Objective-C协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到Objective-C协议的使用以如下方式使用:

  @protocol MyProtocol < NSObject> 

@required

@property(readonly)NSString * title;

@optional

- (void)someMethod;

@end

我看过这个格式,子类扩展的具体超类。问题是,如果你符合这个协议,你需要自己合成属性吗?如果你扩展一个超类,答案显然不是,你不需要。但是如何处理协议需要符合的属性?



根据我的理解,你仍然需要在对象的头文件中声明实例变量符合需要这些属性的协议。在这种情况下,我们可以假设他们只是一个指导原则吗?几乎不是所需方法的情况。编译器将扣掉你的手腕,排除协议列出的必需方法。



这里有一个例子,生成一个编译错误(注意:我已经修剪的代码,没有反映手头的问题) :



MyProtocol.h

  @protocol MyProtocol< NSObject& 

@required
@property(nonatomic,retain)id anObject;

@optional

TestProtocolsViewController.h

   - (void)iDoCoolStuff; 

@end

#import< MyProtocol.h>

@interface TestProtocolsViewController:UIViewController< MyProtocol> {

}

@end

TestProtocolsViewController .m

  #importTestProtocolsViewController.h

@implementation TestProtocolsViewController
@synthesize一个东西; // anObject不存在,即使我们符合MyProtocol。

- (void)dealloc {
[anObject release]; // anObject不存在,即使我们符合MyProtocol。
[super dealloc]
}

@end


解决方案>

协议只是告诉每个人知道你的类通过协议,属性 anObject 将在那里。协议不是真的,它们没有变量或方法本身 - 它们只描述一组特定的属性,这是真正的关于你的类,以便拥有引用它们的对象可以以特定的方式使用它们。



这意味着在你的类中,符合你的协议,你必须做的一切,以确保anObject工作。



@property @synthesize 为您生成代码的机制。 @property 只是说会有一个属性名的getter(和/或setter)方法。这些天 @property 单独是足够的也有方法和一个存储变量为你创建的系统(你曾经需要添加 @sythesize )。但是你必须有一些东西来访问和存储这个变量。


I've seen usage of Objective-C protocols get used in a fashion such as the following:

@protocol MyProtocol <NSObject>

@required

@property (readonly) NSString *title;

@optional

- (void) someMethod;

@end

I've seen this format used instead of writing a concrete superclass that subclasses extend. The question is, if you conform to this protocol, do you need to synthesize the properties yourself? If you're extending a superclass, the answer is obviously no, you do not need to. But how does one deal with properties that a protocol requires to conform to?

To my understanding, you still need to declare the instance variables in the header file of an object that conforms to a protocol that requires these properties. In that case, can we assume that they're just a guiding principle? CLearly the same isn't the case for a required method. The compiler will slap your wrist for excluding a required method that a protocol lists. What's the story behind properties though?

Here's an example that generates a compile error (Note: I've trimmed the code which doesn't reflect upon the problem at hand):

MyProtocol.h

@protocol MyProtocol <NSObject>

@required
@property (nonatomic, retain) id anObject;

@optional

TestProtocolsViewController.h

- (void)iDoCoolStuff;

@end

#import <MyProtocol.h>

@interface TestProtocolsViewController : UIViewController <MyProtocol> {

}

@end

TestProtocolsViewController.m

#import "TestProtocolsViewController.h"

@implementation TestProtocolsViewController
@synthesize anObject; // anObject doesn't exist, even though we conform to MyProtocol.

- (void)dealloc {
    [anObject release]; //anObject doesn't exist, even though we conform to MyProtocol.
    [super dealloc];
}

@end     

解决方案

The protocol is just telling everyone that knows about your class through the protocol, that the property anObject will be there. Protocols are not real, they have no variables or methods themselves - they only describe a specific set of attributes that is true about your class so that objects holding references to them can use them in specific ways.

That means in your class that conforms to your protocol, you have to do everything to make sure anObject works.

@property and @synthesize are at heart two mechanisms that generate code for you. @property is just saying there will be a getter (and/or setter) method for that property name. These days @property alone is enough to also have methods and a storage variable created for you by the system (you used to have to add @sythesize). But you have to have something to access and store the variable.

这篇关于如何处理包含属性的Objective-C协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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