Extract define
The Extract Define refactoring method defines the selected set of tokens as macro using #define
directive and replaces the set of tokens with macro call in the code; macro definition can be located in the current file or moved to the related header file.
Extract #define
In the editor, select the expression you want to replace with a macro call.
Press ⌃ ⌥ D or select
from the main or context menu.If there are several expressions available for extracting, select the required one from the list that opens and press ⏎.
If more than one occurrence of the selected expression is found, AppCode will suggest replacing all the occurrences or just the selected one. Select the desired option from the list that opens.
Select a name for the macro from the list of suggestions that opens or type a new one.
Select Put to header if you want to move the macro definition to the header file.
Code example
Before | After |
---|---|
@implementation Screen{
}
- (CGPoint)getScreenCenter {
// [UIScreen mainScreen].bounds.size will be extracted
return CGPointMake(0.5 * [UIScreen mainScreen].bounds.size.width,
0.5 * [UIScreen mainScreen].bounds.size.height);
}
@end
|
// Extracted #define
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
@implementation Screen{
}
- (CGPoint)getScreenCenter {
return CGPointMake(0.5 * SCREEN_SIZE.width,
0.5 * SCREEN_SIZE.height);
}
@end
|