Delegate Protocol Announcement

I would like to know what the difference is when declaring protocolin the same class and declaring it in a separate file; Example:

#import <UIKit/UIKit.h>

@class MyClassA;

@protocol MyDelegate <NSObject>

@required
- (MyClassA*)myMythod;

@optional
- (void)anOtherMethod:(NSString*)ID;

@end

@interface MyClassB : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) id <MyDelegate> delegate;
......

here I declare the delagate protocol in the same file with MyClassB, and I can declare it (protocol delegate) in a separate source file. What is the difference between declaring it in a single file with a class and a separate file? Thank!

+5
source share
3 answers

There are definitely subtle differences.

, , , , MySpecialViewController MySpecialViewControllerDelegate, . , , , , MySpecialViewController. , .

( ) . , . , #import . #import .m, , , API.

- , . , Parent Child , , #import "Child.h". Child foo:bar: Parent. FooProtocol:

@protocol FooProtocol
  - (void) foo: (int) arg1 bar: (BOOL) arg2;
@end

Parent.h:

@interface Parent : SomeBaseClass<FooProtocol> {
}

Child :

@interface Child {
}
@property (assign) id<FooProtocol> fooHandler;

[fooHandler foo: 1 bar: YES];

Parent ( Parent.h). , FooProtocol FooProtocol.h, Parent.h. , FooProtocol - Child, Child.h, , , , Child.

, , , .

+8

. , .

, , " " ( , :-)) . , , , , .

+1

, , , , , .

The convention is supposed to include the appropriate protocols inside the class header file, since it is more organized and stored together, but if you have extremely large protocols, it might make sense to have them in separate files to make your class headers easier to read.

0
source

All Articles