开发者

Objective-C Removing Inherited Methods from a class

For a subclass, is there way I can remove/stop methods from super class implementation?

开发者_Python百科

(ie. nsstring has a method length. i want to stop "mystring" class from attaining the length method).


length is a primitive method in the NSString class cluster; pretty much every other method in NSString is implemented using calls to length.

Instead of going down this path of madness, make your class a subclass of NSObject. Without a length method, it isn't really a valid NSString subclass anyway.


You can override the method to call doesNotRecognizeSelector::

- (NSUInteger)length
{
    [self doesNotRecognizeSelector:_cmd];
}

But if you have to do this, you may want to rethink whether you really want to subclass in the first place. It may be better to use composition instead of inheritance if you only want to "inherit" specific behaviors/methods.


You override respondsToSelector: to return NO for the appropriate selectors and override the method to call doesNotRecognizeSelector:.


The better answer is "don't do that". Don't try removing methods, instead put an NSAssert(NO, @"don't call me") in your class's implementation, and never call it yourself.

If you must make a "thing that is like X but doesn't do Y", make a new class, and delegate behavior to an instance of the real X.


Can't you simply override the method ?


You can use doesNotRecognizeSelector:. From the documentation:

- (id)copy
{
    [self doesNotRecognizeSelector:_cmd];
}

However, there's a good chance that this is the Wrong Thing To Do. The interface for a class is a contract; it states that an instance that is a kind of that class (which includes subclass instances) will accept that message. When your MyString is passed as an NSString and it doesn't accept the length message, it's violating the contract and will likely cause the program to crash, since no one would think to handle an NSInvalidArgumentException from invoking length on an NSString.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜