Extract protocol
With the Extract Protocol refactoring you can create a new protocol based on the members of the current class.
Perform the Extract Protocol refactoring
Select the desired class in the editor.
Select
from the main or context menu.In the dialog that opens, type the name of the new protocol, select the members that you want to move there, and click Extract.
Code example
Before | After |
---|---|
// Dog.h
@interface Dog
// This method will be extracted
// to a protocol
- (void)swim;
- (void)run;
- (void)eat;
@end
// Dog.m
@implementation Dog {
}
- (void)swim {
NSLog(@"I am swimming");
}
- (void)run {
NSLog(@"I am running");
}
- (void)eat {
NSLog(@"I am eating");
}
@end
|
// New protocol
// Swimmable.h
@protocol Swimmable <NSObject>>
- (void)swim;
@end
// Dog.h
@interface Dog <Swimmable>
- (void)run;
- (void)eat;
@end
// Dog.m
@implementation Dog {
}
- (void)swim {
NSLog(@"I am swimming");
}
- (void)run {
NSLog(@"I am running");
}
- (void)eat {
NSLog(@"I am eating");
}
@end |
Last modified: 14 December 2022