< Previous PageNext Page > Hide TOC

Refactoring Transformations

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.

In this section:

Rename
Extract
Encapsulate
Create Superclass
Move Up
Move Down
Modernize Loop
Convert to Objective-C 2.0


Rename

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:

The project elements this transformation modifies include:

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;
}

Extract

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:

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.

Encapsulate

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.

Note: This transformation can operate on instance variables, too.

This transformation contains the following specifiers:

Create Superclass

The Create Superclass transformation creates a superclass for the selected class.

This transformation contains the following specifiers:

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.

Move Up

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 Down

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:

Note: This refactoring removes the transformation item’s declaration/definition from the class that declares/defines it.

Modernize Loop

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:

These are additional restrictions on for loops:

These are additional restrictions on while loops:

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);
   }
}

Convert to Objective-C 2.0

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:



< Previous PageNext Page > Hide TOC


© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-01-06)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.