Sunday, May 24, 2015

A Simple Swift iOS App from Start to Finish - Refactoring the ViewController's Name

Introduction

This is part of a series of articles detailing the creation of a simple iOS app. A complete list  of articles is included in the first part A Simple Swift iOS App from Start to Finish - Introduction.


I have set up a web site for the finished app at http://DaysWithoutThings.com and you can download it free directly from the app store:



The Single View Application template created a View Controller class called ViewController for me. However as I will be adding two other View Controllers to my project I want to change this class name to the slightly less generic MainViewController.


Refactoring in Xcode

Xcode has a built in refactoring capability which includes an option for changing the name of a class. Unfortunately it is currently only available for C and Objective-C not Swift. Because of this I am going to have to manually replicate what the refactor-renaming process does.

Searching for the name to change

The first thing the refactor-renaming process does is search through the projects files looking for references to the name that will be changed. I can do this too using the Find navigator.


I can see from this that there are two references to the class name. One is the class definition in ViewController.swift and the other is setting the class property of the view controller in the storyboard.

Renaming the class

When I click on the reference to the class definition Xcode opens that file in its main editor. I can then just add my main prefix to the class name.



I am going to switch back to the Project navigator where I can see the file is now marked with the source control M for modified. 



It is not actually necessary for the file name to match the class name it contains. However it is less confusing so I am going to do that. To do this I select the file then pause and then click it again just like changing a file name in Finder.





When I finish editing the file name the Source Control icon changes to A+. This means that ,at the next commit, the file with the old name will be deleted and the file with the new name will be added.





This just leaves the reference in the storyboard. To find that reference I will switch back to the Find navigator and click on the reference there. This opens Interface Builder and selects the View Controller object and highlights its custom class property.


I can then change the property by selecting my new class name in the drop down list.




Wrapping up

At this point I am going to rerun my app in the iOS simulator just to check that I have not broken anything. Once I am sure it is all working I will commit the changes I have made to the Source Control repository.

In my next article  I will move on to adding the icon artwork required for apps.

Next:       A Simple Swift iOS App from Start to Finish - Creating App Icons with InkScape

No comments:

Post a Comment