Super methods to be subclassed anyway (Cocoa)

Sorry if this is repost, but I could not find it, because I can not explain it in a few words. I have a superclass with many methods, but they will always (not all) be subclassed. From super I need to run these methods. I could either leave the methods in super empty, or just not call them super, but call them the same way as [self myMethod]it would, and it will call my subclass method, even if it does not exist in super. This works, but Xcode gives me an error.'superclass' may not respond to '-subclassmethod'

What should I do, I will not receive a warning?

+3
source share
4 answers

:

@interface GLObject : NSObject {}
- (id)someSubclassProvidedMethod;
@end

@implementation GLObject
- (id)someSubclassProvidedMethod {
  [self doesNotRecognizeSelector: _cmd];
}
@end

, Objective-C -doesNotRecognizeSelector:, . , , .

+4

protocol, "" .

@protocol MyProtocol
-(id)myMethodWith:(id)arg;
@end

, , .

-(id)doStuffWith:(SuperClass <MyProtocol> *)aThing and:(id)another {
    return [aThing myMethodWith:another]
}

, SuperClass doStuffWith:and:, MyProtocol, , .

+1

, :

@protocol JSDog <NSObject>
- (void)yipe;
@end

@interface JSDog : NSObject
@end

@implementation JSDog

+ (void)initialize {
  if ([self isSubclassOfClass:[JSDog class]] && ![self conformsToProtocol:@protocol(JSDog)]) {
    NSAssert(false, @"Subclasses of JSDog must conform to <JSDog>.");
  }
}

@end

, , NSObject. a @required, : , JSDog <JSDog>, doesn ' t -yipe, ; , <JSDog>, .

+1

NSAssert :

- (BOOL)proceedForVersion:(int)versionInteger
{
    NSAssert(false, @"This method needs to be overridden in a subclass of iMBApertureAbstractParser");

    return NO;
}
0

Source: https://habr.com/ru/post/1739824/


All Articles