jfalexvijay
jfalexvijay

Reputation: 3711

How to fix the warnings

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 ?

Upvotes: 0

Views: 148

Answers (2)

Xval
Xval

Reputation: 938

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;

Upvotes: 0

zoul
zoul

Reputation: 104065

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];
}

Upvotes: 3

Related Questions