Xcode performs transformation operations (refactorings) within the current project; it doesn’t perform transformations across project references.
This chapter describes each of the refactorings Xcode helps you perform.
Rename
Extract
Encapsulate
Create Superclass
Move Up
Move Down
Modernize Loop
Convert to Objective-C 2.0
The Rename transformation renames the transformation item throughout the project files.
Note: This transformation can rename items other than methods, such as functions, structures, structure fields, and so on.
This transformation contains the following specifiers and options:
New Name. The new name for the transformation item.
Rename related KVC/Core Data Members. Specifies whether to change related KVC methods and Core Data properties.
Rename Related Files. Available when the transformation item is declared in a header file named after the transformation item, and defined in the corresponding implementation file.
The project elements this transformation modifies include:
The transformation item’s declaration/definition
Related KVC methods and Core Data properties, when indicated
The names of the header and implementation files that define the item and the corresponding import/include statements in files that use the item, when indicated
Code that directly references the transformation item
Listing 3-1 and Listing 3-2 show an example of a rename transformation.
Listing 3-1 Renaming an index variable in a for
loop (before)
- (int) myMethod { |
int j = 1; |
int i; |
i = 5; |
if (j == 1) { |
int i; |
for (i = 0; i < 10; i++) { |
printf("Item index: %i\n", i); |
... |
} |
} |
return i; |
} |
Listing 3-2 Renaming an index variable in a for
loop (after)
- (int) myMethod { |
int j = 1; |
int i; |
i = 5; |
if (j == 1) { |
int item_index; |
for (item_index = 0; item_index < 10; item_index++) { |
printf("Item index: %i\n", item_index); |
... |
} |
} |
return i; |
} |
The Extract transformation creates a function or method with the selected code as its body.
Xcode analyzes the context of the selected code and the variables it uses to determine the generated routine’s parameters and return value.
This transformation contains the following specifiers:
Extracted Routine Name. The name of the function or method, including parameter names and types and return-value type, which you can customize according to your preferences.
Extract To. The type of routine to which the selected code is to be extracted: a method or a function.
Note: When extracting code from a method into a function, this transformation generates parameters for all the implicit data the code uses that would otherwise be inaccessible to a function.
The Encapsulate transformation creates accessors for the transformation item, reduces its visibility, and changes code that directly accesses the item to use the accessors instead.
This transformation contains the following specifiers:
Getter. The method to use to get the value of the transformation item.
Setter. The method to use to set the value of the transformation item.
The Create Superclass transformation creates a superclass for the selected class.
This transformation contains the following specifiers:
Superclass Name. The name of the new superclass for the selected class.
Declaration and Definition Location. You can choose between placing the new class’s declaration and definition in:
The the same header and implementation files where the selected class is declared/defined
New header and implementation files
To complete the transformation, you may need to correct the import/include statements of the source files that declare/define the selected class and the new header/implementation files.
The Move Up transformation moves the declaration and definition of the transformation item to the superclass of the class that declares and defines the item.
This transformation contains the following option:
Move Up Related Methods. Specifies whether to move methods that directly access the transformation item—and are declared/defined in the class that defines the item—to the superclass, too.
The Move Down transformation moves the declaration and definition of the transformation item to one or more of the subclasses of the class that declares/defines the item.
This transformation contains the following specifier:
Subclasses to Move the Item To. List of classes to which the transformation item is moved.
Note: This refactoring removes the transformation item’s declaration/definition from the class that declares/defines it.
The Modernize Loop transformation modifies the selected loop to use the less verbose and more efficient Objective-C 2.0 for
loop.
This transformation operates only on a loop that meets the following requirements:
The loop iterates over all the elements of a collection: an NSArray
or NSSet
object.
The loop accesses each item in the collection in sequential order, starting at the first item.
Each of the loop’s iterations processes only one item of the collection at a time; it doesn’t access any preceding or succeeding items.
These are additional restrictions on for
loops:
The loop iterates over the elements of an NSArray
object.
The loop uses a variable as the index into the collection, and this variable goes from 0
to [<collection> count] - 1
.
The loop gets the current element with [<collection> objectAtIndex:<index>]
and, either saves it once into an element variable that’s accessed in the rest of the loop’s body, or uses this expression to retrieve the current element throughout the loop’s body.
These are additional restrictions on while
loops:
The loop uses an NSEnumerator object to iterate over the collection.
The loop gets the current element with [<enumerator> nextObject]
and exits when the current element is nil
.
The loop does not change the loop control variables.
The loop’s containing code does not access the loop’s control variables.
Listing 3-3 and Listing 3-4 show a Modernize Loop transformation on a for
loop.
Listing 3-3 Modernizing a for
loop (before)
{ NSArray *array = ...; |
NSObject *object = ...; |
int index; |
int array_count = [array count]; |
for (index = 0; index < array_count; index++) { |
[object someMethod:[array objectAtIndex:index]]; |
NSLog(@"%@, [array, objectAtIndex:index]); |
} |
} |
Listing 3-4 Modernizing a for
loop (after)
{ NSArray *array = ...; |
NSObject *object = ...; |
for (foo in array) { |
[object someMethod:foo]; |
NSLog(@"%@, foo]); |
} |
} |
Listing 3-5 and Listing 3-6 show a Modernize Loop transformation on a while
loop.
Listing 3-5 Modernizing a while
loop (before)
{ NSSet *set = ...; |
NSEnumerator *enumerator = [set objectEnumerator]; |
NSObject *item; |
while ((item = [enumerator nextObject]) != nil) { |
NSLog(@"%@", item); |
} |
} |
Listing 3-6 Modernizing a while
loop (after)
{ NSArray *set = ...; |
NSObject *item; |
for (item in set) { |
NSLog(@"%@", item); |
} |
} |
The Convert to Objective-C 2.0 transformation modifies all the source files of the current project to take advantage of features that Objective-C 2.0 introduces.
This transformation has the following specifiers:
Modernize Loops. Specifies whether to perform the transformation on all source code files.
Use Properties. Specifies whether to replace instance variables with Objective-C properties.
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-01-06)