Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Refactoring

We have a working method now, but it’s implemented on the same class as the test cases for that method. The temperature converter and the tests of its behavior are separate concerns, so should be defined in separate classes. In fact, dividing the responsibilities between two different classes means we don’t need to ship the test class at all, which is beneficial because the user doesn’t particularly need our test code. Because the converter method needs to use the text field and the label from the converter view, it makes sense to put it into a view controller that will manage the view.

@interface TemperatureConverterViewController : UIViewController <UITextFieldDelegate>
@property (strong) IBOutlet UITextField *celsiusTextField;
@property (strong) IBOutlet UILabel *fahrenheitLabel;
@end

@implementation TemperatureConverterViewController

@synthesize celsiusTextField;
@synthesize fahrenheitLabel;

- (BOOL)textFieldShouldReturn: (id)celsiusField {
    double celsius = [[celsiusField text] doubleValue];
    double fahrenheit = celsius * (9.0/5.0) + 32.0;
    fahrenheitLabel.text = [NSString stringWithFormat: @"%.0f", fahrenheit];
    return YES;
}

@end


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial