在Objective-C中定义协议的类别? [英] Defining categories for protocols in Objective-C?
问题描述
在Objective-C中,我可以将方法添加到具有类别的现有类中,例如
@interface NSString (MyCategory)
- (BOOL) startsWith: (NSString*) prefix;
@end
是否也可以使用协议来执行此操作,即是否存在NSString协议,例如:
@interface <NSString> (MyCategory)
- (BOOL) startsWith: (NSString*) prefix;
@end
我想这样做,因为我有多个NSObject(类)扩展,仅使用公共NSObject方法,并且我希望这些扩展也可以与实现协议的对象一起使用.
再举一个例子,如果我想编写一个方法logDescription,该方法将对象的描述打印到日志中:
- (void) logDescription {
NSLog(@"%@", [self description]);
}
我当然可以将此方法添加到NSObject,但是还有其他一些类不继承自NSObject,我也想在其中使用此方法,例如NSProxy.由于该方法仅使用协议的公共成员,因此最好将其添加到协议中.
Java 8现在在接口中具有虚拟扩展方法": http://cr.openjdk.java.net/~briangoetz/lambda/Defender%20Methods%20v4.pdf .这正是我要在Objective-C中执行的操作.我没有看到这个问题获得如此多的关注...
关于, 乔钦(Jochen)
extObjC具有最简单的东西您可以使用Protocols/类别...首先是@concreteprotocol
...
- 定义具体协议",它可以提供协议中方法的默认实现.
- 头文件中应存在一个
@protocol
块,而在实现文件中应存在一个相应的@concreteprotocol
块. - 任何声明自己符合此协议的对象都将接收其方法实现,但前提是不存在同名的方法.
MyProtocol.h
@protocol MyProtocol
@required - (void)someRequiredMethod;
@optional - (void)someOptionalMethod;
@concrete - (BOOL)isConcrete;
MyProtocol.m
@concreteprotocol(MyProtocol) - (BOOL)isConcrete { return YES; } ...
所以声明一个对象MyDumbObject : NSObject <MyProtocol>
将自动将YES
返回到isConcrete
.
此外,它们还有pcategoryinterface(PROTOCOL,CATEGORY)
,它在协议PROTOCOL上为名为CATEGORY的类别定义接口".协议类别包含的方法会自动应用于声明自己符合PROTOCOL的任何类."在实现文件中也必须使用一个附带的宏.请参阅文档.
@protocols
相关的最后但并非最不直接的 是
synthesizeAssociation(CLASS, PROPERTY)
,它使用关联的对象为类合成属性.这对于将属性添加到类别中的类非常有用.必须在指定类(或类别)的接口中使用@property
声明PROPERTY. ),并且必须是对象类型."
该库中的许多工具打开(逐步)了您可以使用ObjC进行的工作...从多重继承到很好,您的想象力是极限. /p>
In Objective-C, I can add methods to existing classes with a category, e.g.
@interface NSString (MyCategory)
- (BOOL) startsWith: (NSString*) prefix;
@end
Is it also possible to do this with protocols, i.e. if there was a NSString protocol, something like:
@interface <NSString> (MyCategory)
- (BOOL) startsWith: (NSString*) prefix;
@end
I want to do this since I have several extensions to NSObject (the class), using only public NSObject methods, and I want those extensions also to work with objects implementing the protocol .
To give a further example, what if I want to write a method logDescription that prints an object's description to the log:
- (void) logDescription {
NSLog(@"%@", [self description]);
}
I can of course add this method to NSObject, but there are other classes that do not inherit from NSObject, where I'd also like to have this method, e.g. NSProxy. Since the method only uses public members of protocol , it would be best to add it to the protocol.
Edit: Java 8 now has this with "virtual extension methods" in interfaces: http://cr.openjdk.java.net/~briangoetz/lambda/Defender%20Methods%20v4.pdf. This is exactly what I would like to do in Objective-C. I did not see this question earning this much attention...
Regards, Jochen
extObjC has the NEATEST stuff you can do with Protocols / Categories... first off is @concreteprotocol
...
- Defines a "concrete protocol," which can provide default implementations of methods within protocol.
- An
@protocol
block should exist in a header file, and a corresponding@concreteprotocol
block in an implementation file. - Any object that declares itself to conform to this protocol will receive its method implementations, but only if no method by the same name already exists.
MyProtocol.h
@protocol MyProtocol
@required - (void)someRequiredMethod;
@optional - (void)someOptionalMethod;
@concrete - (BOOL)isConcrete;
MyProtocol.m
@concreteprotocol(MyProtocol) - (BOOL)isConcrete { return YES; } ...
so declaring an object MyDumbObject : NSObject <MyProtocol>
will automatically return YES
to isConcrete
.
Also, they have pcategoryinterface(PROTOCOL,CATEGORY)
which "defines the interface for a category named CATEGORY on a protocol PROTOCOL". Protocol categories contain methods that are automatically applied to any class that declares itself to conform to PROTOCOL." There is an accompanying macro you also have to use in your implementation file. See the docs.
Last, but NOT least / not directly related to @protocols
is
synthesizeAssociation(CLASS, PROPERTY)
, which "synthesizes a property for a class using associated objects. This is primarily useful for adding properties to a class within a category. PROPERTY must have been declared with @property
in the interface of the specified class (or a category upon it), and must be of object type."
So many of the tools in this library open (way-up) the things you can do with ObjC... from multiple inheritance... to well, your imagination is the limit.
这篇关于在Objective-C中定义协议的类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!