如何修复警告

发布于 2024-10-05 14:59:07 字数 359 浏览 0 评论 0原文

在我的 iPhone 项目中,我收到“找不到方法名称”、“找到多个名为“method_name”的方法”警告消息。


// in TestFirst.h
-(void) testMethod:(int)a;
// in TestFirst.m TestSecond *ts = [[TestSecond alloc] init]; ts.delegate = self;
// in TestSecond.h id delegate;
// in TestSecond.m [delegate testMethod: 5]; // Warning: method name not found

如何解决此类警告?

In my iPhone project, I am getting "method name not found", "multiple methods named 'method_name' found" warning message.


// in TestFirst.h
-(void) testMethod:(int)a;
// in TestFirst.m TestSecond *ts = [[TestSecond alloc] init]; ts.delegate = self;
// in TestSecond.h id delegate;
// in TestSecond.m [delegate testMethod: 5]; // Warning: method name not found

How to resolve this kind of warnings ?

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

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

发布评论

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

评论(2

留蓝 2024-10-12 14:59:07

您可以为委托提供精确的类型:

TestFirst *delegate;

或者您可以创建一个协议:

@protocol SomeDelegate
- (void) testMethod: (int) a;
@end

@interface TestFirst : NSObject <SomeDelegate> {…}
@end

@interface TestSecond : NSObject
@property(assign) id <SomeDelegate> delegate;
@end

或者您可以保留动态类型并导入正确的标头:

@interface TestSecond : NSObject {…}
@property(assign) id delegate;
@end

#import "TestFirst.h" // or AVAudioPlayer or whatever
@implementation TestSecond

- (void) somewhere {
    [delegate testMethod:5];
}

You can give a precise type for the delegate:

TestFirst *delegate;

Or you can create a protocol:

@protocol SomeDelegate
- (void) testMethod: (int) a;
@end

@interface TestFirst : NSObject <SomeDelegate> {…}
@end

@interface TestSecond : NSObject
@property(assign) id <SomeDelegate> delegate;
@end

Or you can keep the dynamic typing and import the correct headers:

@interface TestSecond : NSObject {…}
@property(assign) id delegate;
@end

#import "TestFirst.h" // or AVAudioPlayer or whatever
@implementation TestSecond

- (void) somewhere {
    [delegate testMethod:5];
}
蹲在坟头点根烟 2024-10-12 14:59:07

这可能不是最好的方法,但我见过大多数人使用以下模式使用委托:

if ([delegate respondsToSelector:@selector(yourMethod)]) {
     [delegate performSelector:@selector(yourMethod)];
}

您可以使用 PerformSelector:withObject: 添加参数,还有一些方法允许您在其他线程中执行选择器。

声明您的委托,则不会有任何错误

id delegate;

如果您像或那样

NSObject<DelegateProtocol> * delegate;

It might not being the best way to do it but i've seen most people using delegates using the following pattern :

if ([delegate respondsToSelector:@selector(yourMethod)]) {
     [delegate performSelector:@selector(yourMethod)];
}

You can add arguments using performSelector:withObject: and there are also methods allowing you to perform the selector in other threads.

You won't have any errors if you declare your delegate like

id delegate;

or

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