Convert
Convert refactorings allow converting methods to functions or blocks and vice versa, as well as converting properties to instance variables.
These refactorings are invoked from the Refactor menu. You can also use the Change Signature refactoring for the same purposes.
Convert to method
The Convert to Method refactoring lets you convert a function or block into a method.
In the editor, place the caret at the function or block that you want to convert into a method.
From the main or context menu, select
.In the dialog that opens, make changes if necessary:
To perform the refactoring, click Refactor. To see expected changes and make necessary adjustments prior to performing the refactoring, click Preview.
Before | After |
---|---|
// This function will be converted into a method
BOOL isPasswordValid(NSString *password) {
return password.length > 4;
}
- (void)performLogin {
// ...
// Function's usage
if (isPasswordValid(password) == NO) {
// ...
}
}
|
// New method
+ (BOOL)isPasswordValid:(NSString *)password {
return password.length > 4;
}
- (void)performLogin {
// ...
// Method's usage
if ([LoginViewController isPasswordValid:password] == NO) {
// ...
}
}
|
Convert to function
The Convert to Function refactoring lets you convert a method or block to a function.
In the editor, place the caret at the method or block that you want to convert into a function.
From the main or context menu, select
.In the dialog that opens, make changes if necessary:
To perform the refactoring, click Refactor. To see the expected changes and make the necessary adjustments prior to performing the refactoring, click Preview.
Before | After |
---|---|
// This method will be converted into a function
- (void)setIsValid:(BOOL)isValid forField:(UITextField *)field {
field.textColor = isValid ? [UIColor blackColor] : [UIColor redColor];
}
- (void)performLogin {
// ...
if (isEmailValid == NO) {
// Method's call
[self setIsValid:NO forField:self.textFieldEmail];
}
}
|
// New function
void setIsValid(BOOL isValid, UITextField *field) {
field.textColor = isValid ? [UIColor blackColor] : [UIColor redColor];
}
- (void)performLogin {
// ...
if (isEmailValid == NO) {
// Functions's call
setIsValid(NO, self.textFieldPassword);
}
}
|
Convert to block
The Convert to Block refactoring lets you convert a function or method into a block.
In the editor, place the caret at the method or function that you want to convert into a block.
From the main or context menu, select
.In the dialog that opens, make changes if necessary:
To perform the refactoring, click Refactor. To see the expected changes and make the necessary adjustments prior to performing the refactoring, click Preview.
Before | After |
---|---|
// This function will be converted into a block
BOOL isPasswordValid(NSString *password) {
return password.length > 4;
}
- (void)performLogin {
// ...
// Function's call
if (isPasswordValid(password) == NO) {
// ...
}
}
|
- (void)performLogin {
// ...
// New block
if (^BOOL(NSString *password) {
return password.length > 4;
}(password) == NO)
// ...
}
|
Convert to property
The Convert to Property refactoring lets you convert an instance variable into a property.
Place the caret at the instance variable that you want to convert into a property.
From the main or context menu, select
.In the dialog that opens, select instance variable(s) you want to convert:
Click OK to perform the refactoring.
Before | After |
---|---|
@interface AlertParams : NSObject {
// These instance variables
// will be converted to properties
@public
NSString *_title;
NSString *_message;
}
@end
|
@interface AlertParams : NSObject
// New properties
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *message;
@end
|
Convert to instance variable
The Convert to Instance Variable refactoring lets you convert a property to an instance variable.
Place the caret within the editor window.
From the main or context menu, select
.In the dialog that opens, select the properties you want to convert:
Click OK to perform the refactoring.
Before | After |
---|---|
@interface AlertParams : NSObject
// These properties will be converted
// to instance variables
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *message;
@end
|
@interface AlertParams : NSObject {
// New instance variables
@public
NSString *_title;
NSString *_message;
}
@end
|