super 中的方法无论如何都会被子类化 (Cocoa)

发布于 2024-08-28 09:35:19 字数 288 浏览 11 评论 0原文

抱歉,如果这是转发,但我无法完全搜索它,因为我无法用几句话解释它。我有一个包含很多方法的超类,但它们总是(不是全部)被子类化。我需要从超级运行这些方法。我可以将 super 中的方法保留为空,或者我可以不在 super 中键入它们,但无论如何都可以调用它们,就像 [self myMethod] 一样,它会调用我的子类方法,即使它不存在于极好的。这可行,但 Xcode 给了我一个错误。 'superclass' 可能不会响应 '-subclassmethod'

我应该怎么做才能不会收到警告?

Sorry if this is a repost but I couldn't quite search for it because I can't explain it in a few words. I have a super class with lots of methods but they will always (not all of them) be subclassed. From the super I need to run those methods. I could either leave the methods in super empty or I could just not type them in super but call them anyway like so [self myMethod] and it will call my subclassed method even if it doesn't exist in super. This works but Xcode gives me an error though. 'superclass' may not respond to '-subclassmethod'

What should I do so I won't get the warnings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请别遗忘我 2024-09-04 09:35:19

我更喜欢像这样在超类中定义未实现的方法:

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

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

这几乎完全是多余的,因为如果我根本没有定义该方法,Objective-C 运行时最终会调用 -doesNotRecognizeSelector: 。但因为我确实定义了它,所以它位于类的接口中,这既使编译器满意,又为我提供了一些文档。

I prefer to define the unimplemented methods in the superclass like this:

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

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

It's almost entirely redundant, because the Objective-C runtime would eventually call -doesNotRecognizeSelector: if I didn't define the method at all. But because I do define it, it's in the class's interface which both keeps the compiler happy and provides me with some documentation.

通知家属抬走 2024-09-04 09:35:19

您可以在 协议,其他语言中称为“接口”。

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

更改变量的类型声明以声明该对象符合协议。

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

请注意,您将无法将 SuperClass 的实例传递给 doStuffWith:and:,因为它不会实现 MyProtocol,但听起来这就是您想要的。

Rather than the superclass, you could declare the methods in a protocol, what is called a "interface" in other languages.

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

Change the type declaration of the variables to declare that the object conforms to the protocol.

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

Note that you won't be able to pass an instance of your SuperClass to doStuffWith:and:, since it won't implement MyProtocol, but it sounds like that's what you want.

萌吟 2024-09-04 09:35:19

我的解决方案有点奇怪,但它是:

@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 中拥有与类同名的协议。因为正式协议中的方法默认为 @required,所以您将在两端受到保护:在编译时中,如果您的 JSDog 子类声称符合 ,但未实现 -yipe,您将收到错误;在运行时,如果您的子类未声明符合,则在实例化子类时您将收到警告。

My solution was a little weird, but here it is:

@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

Having a protocol with the same name as a class is precedented in NSObject. Because methods in a formal protocol a by default @required, you will be protected on both ends: in compile-time, if your JSDog subclass purports to conform to <JSDog>, but doesn't implement -yipe, you will receive an error; at runtime, if your subclass does not claim to conform with <JSDog>, you will receive a warning when the subclass is instantiated.

智商已欠费 2024-09-04 09:35:19

我最近喜欢使用 NSAssert 来完成此任务:

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

    return NO;
}

I lately like using NSAssert for this task:

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

    return NO;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文