For a subclass, is there way I can remove/stop methods from super class implementation?
(ie. nsstring has a method length. i want to stop "mystring" class from attaining the length method).
For a subclass, is there way I can remove/stop methods from super class implementation?
(ie. nsstring has a method length. i want to stop "mystring" class from attaining the length method).
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:
.
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.
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.
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.