AppCode 2022.2 Help

Extract protocol

With the Extract Protocol refactoring you can create a new protocol based on the members of the current class.

Extract protocol

Perform the Extract Protocol refactoring

  1. Select the desired class in the editor.

  2. Select Refactor | Extract/Introduce | Protocol from the main or context menu.

  3. 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: 08 March 2021