English 中文(简体)
我如何界定一个类别,以补充执行某一议定书的类别的方法?
原标题:How do I define a category that adds methods to classes which implement a particular protocol?

我要补充一些方法,以分类执行SOManagedObject议定书的NSManagedObject。 我试图这样界定:

@interface NSManagedObject <SOManagedObject> (MyExtensionMethods)
    ...
@end

...but that doesn t seem to be valid. How do I define a category that adds methods to classes which implement a particular protocol?

最佳回答

在所有这些类别中界定这种类别不易被搁置。 但你的实际问题似乎更为简单: 如何在<代码>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.

问题回答

我认为这是不可能的。 此前曾回答过类似问题:

Edit:我错过了问题;我认为,如果没有目标C的运行时间,你可以做些什么。 下面请见Dave s评论,说明我的答复为何不正确。


如果你放弃议定书和同类通知,你就能够这样做:

@interface NSManagedObject (MyExtensionMethods) <SOManagedObject>
    ...
@end




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签