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.开发者_StackOverflow社区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 ?
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];
}
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;
精彩评论