在所有这些类别中界定这种类别不易被搁置。 但你的实际问题似乎更为简单: 如何在<代码>NSManagedObject上添加一种类别方法,仅用于实施<代码>和lt的子类; 目标和效果; 这可减半。
你们想做的是,在<代码>上添加方法。 NSManagedObject,然后检查你重新工作的事例能够处理你希望从<发送的信息;SOManaged 目标和编号;
。
Let us suppose that we are given:
/* SOManagedObject.h */
@protocol SOManagedObject
- (void)frobble_so;
- (void)bobble_so;
@end
Now let s add a category method to all NSManagedObject
subclasses that implement SOManagedObject
:
/* NSManagedObject+SOConvenience.h */
@interface NSManagedObject (SOConvience)
/* Frobbles and bobbles - no effect on non-<SOManagedObject>-conforming
* instances. */
- (void)frobbleAndBobble_so;
@end
为了做到这一点,我们采用这样的方法:
/* NSManagedObject+SOConvenience.m */
@implementation NSManagedObject (SOConvenience)
- (void)frobbleAndBobble_so
{
if (![self conformsToProtocol:@protocol(SOManagedObject)]) return;
NSLog(@"%s: Thunderbirds are GO! Let s frobble and bobble!", __func__);
[self frobble_so];
[self bobble_so];
}
@end
You could optionally assert to ensure you are not sending the method to the wrong objects, or you could use respondsToSelector:
instead of checking for protocol conformance. Whatever floats your boat, but this general tack should take you where you want to go.