Xcode 5 Essentials
Xcode 5 is a major step forward for the Apple ecosystem, bringing more possibilities, features, and tools to developers than ever before. With Version 5, Xcode has grown into an extremely powerful IDE. Read this article to learn what's new!
Xcode 5 is full of improvements and additions, both large and small. All of these changes provide enormous assistance to developers. Changes begin in the interface, where the updated look-and-feel style was adjusted to fit into the new iOS 7 ecosystem. Just by touring yourself around, you'll notice a number of stylistic modifications, starting at the welcome screen.
The new additions are apparent upon using Xcode, and once you work with it for a while, you'll discover that in this version Apple targets two major points. The first goal is to relieve developers from various configuration tasks regarding their applications. The other objective it to elevate debugging and testing to a whole new level and provide developers with useful tools that make these tasks easier and more effective.
All of the new features will be introduced to you right away. In short, here is a list of all the new features or ones with essential changes that you'll encounter in this tutorial:
- Automatic Configuration
- Source Control Enhancements
- Interface Builder and Autolayout Enhancements
- Asset Catalog
- Debugger Enhancements
- Debug Gauges
- Testing and Bots
- Compiler Improvements
1. Automatic Configuration
Enabling an Apple service into an application was always a task that required a certain number of steps that should be performed by the developer until the app is fully setup and working. Frameworks need to be linked to the project, entitlements have to be added to the App ID, keys should be appended in the *.plist file, and all tasks must be done according to the service's requirements. The many small steps made this a difficult process.
Xcode 5 removes the hassle of doing all the above from developers, thanks to the Automatic Configuration feature. With Automatic Configuration, the only requirement for developers is their Apple ID (associated to a developer program). Xcode then automatically enables and sets up any services that developers want, by linking frameworks, managing entitlements, and doing anything else necessary for a service to work. Developers really don't need to do anything else, apart from selecting the services that they would love to see in their applications. Even provisioning profiles can be optionally created automatically with the proper settings. This is an awesome feature for developers!
Let's take a more detailed look at Automatic Configuration.
It is straightforward to assign your Apple ID within Xcode. Simply open Xcode menu > Preferences... > Accounts. The Accounts tab is a new feature in Xcode 5.
At the bottom left of the Preferences window there are three buttons: a plus sign, a minus sign, and a gear sign. Click on the Plus sign > Add Apple ID... option. In the form that appears, provide your Apple ID and your password and click Add.
If everything goes as planned, the Apple ID you just added should be listed at the left side of the window. At the right side, if you select your ID and click on the View Details button, another window shows up on the screen.
This one contains all of your signing identities and provisioning profiles. You can create, remove, or revoke signing identities using the appropriate buttons under the signing identities area. Also with the refresh button at the bottom of the window, you can update all changes you perform online on your provisioning profiles.
To delete an Apple ID, simply select the respective ID from the list on the Accounts and click on the Menus button at the bottom left side of it.
The Capabilities tab automatically allows Apple services to be configured. This feature can be found if you click on your project on the Project Navigator.
As you see in the figure, it is just a matter of enabling or disabling a switch button. Capabilities provide automatic configuration support for the following services:
- Game Center
- In-App Purchase
- Background Modes
- Keychain Sharing
- Inter-App Audio
- Data Protection
If you expand any one of those, a description about the selected service and information about the actions becomes visible. For example, if you expand the Game Center service the following figure appears, stating that the "Game Center" entitlement was added to the App ID, the "GameKit" key was added into the info plist file, and the GameKit framework was linked to your project. These are actions and steps that you could do manually, but now Xcode can do it automatically on your behalf! If you so choose, you can continue to do it on your own, but Xcode saves you some time.
In the General tab (the one at the left of the Capabilities tab), inside the Identity section, there is the Team dropdown menu. If you successfully added your Apple ID into Xcode, click it to select a development team exactly as it appears on iTunes Connect. For developers playing solo, their full name only appears. If you want Automatic Configuration to properly work for you, I recommend to select your team (or name) before enabling any service in the Capabilities tab. In case you leave the Team value to None, then when you try to enable a service you will be asked by Xcode to select your team.
Based on the Apple ID in the Accounts tab of Xcode preferences, the signing identity settings and the Capabilities settings, Xcode can create new provisioning profiles for you, using proper settings, instead of doing so manually. Also, fixes are applied to existing provisioning profiles with issues.
To recap, Automatic Configuration saves you from some demanding manual work by doing many steps for you. However, you can still create and set everything manually as you traditionally have done, or do some steps by yourself and let others to be automatically done by Xcode. It's a great feature, use it at your will.
2. Source Control
Source and version control systems are always helpful, and they are mandatory for projects that tend to become large and are developed by a team of programmers. Even lone-rider developers should adopt the habit of using a source control system because it is a great tool, especially when it comes to code change tracking. A source control system allows you to keep track of code changes and to revert back to a more stable version when it's necessary. For team development, it's great because it allows each developer to build a part of the project, simply by creating a copy of it (branch) and without interfering with other developers' code. When various project pieces are ready, they can be put together (merged) into one working version of code. Also, in cases where too many bugs appear, developers can go back to a more stable version and track down the code changes, simply by comparing two versions of the same file. Git is such a source control system.
Xcode 5 gives special gravity to source control. In Xcode 4.6, source control could be found under the File menu. In Version 5 source control enjoys its own top-level menu in the Xcode menu bar.
As you see in the figure, in this menu you can directly see the current project you are working on and access certain operations, such as Commit, Push, New Branch, Switch to branch, Merge from branch, and many more. It is really easy and fast to access the source control system and to keep any changes you want in your repositories.
Speaking of repositories, you can reach them directly when launching Xcode, even from the welcome screen, simply by clicking on the Check out an existing project option.
Navigate to the Check out form, where you can see a list of all of your repositories, your favorites, or the most recent ones. Alternatively, you can type a location where a repository can be found.
Another useful option regarding source control and Xcode 5 is the Blame for this line operation under the Editor menu. This option displays comment information about the current line that the cursor is on and states who made the last comment. This is especially helpful in team projects because it makes it easy to find the developer who last changed the code.
Note that if you try to use the Blame option without committing at least one of your files using the Source Control menu, no information will be shown to you.
Like many other aspects of Xcode, source control is configurable as well. Go to Xcode > Preferences... > Source Control to access the settings of it. If you'd like, you can completely disable the source control for your project (but it's recommended not to do so). Set the options according to your preference and work in your customized environment.
3. Interface Builder and Autolayout
Interface Builder, the GUI designer of Xcode, doesn't seem to have many apparent changes, except for the missing grid and the general aesthetic upgrade. However, one major feature that was added in this version of Xcode is the ability to preview the interface setup for iOS versions equal or earlier than 6.1. All graphic elements (subviews) were redesigned and developers need to know whether the position, the size, or the layout of the subviews works for both iOS 7 and previous versions of iOS.
To use the preview feature on Interface Builder, at first show the Assistant Editor.
If Xcode automatically shows any file containing code on the Assistant Editor, then locate and open the .xib or storyboard file you want to preview manually, as shown in the next figure.
Once you have the GUI file opened in both the Standard and the Assistant Editor, click on the Related Files button > Preview > Your File of the Assistant Editor.
Notice a frame at the bottom-right of the screen, where it says iOS 7.0 and later. Click on it, select the iOS 6.1 and Earlier option and you'll have the preview ready.
Preview is a useful tool for developers who want to keep backwards compatibility in their applications and to support previous iOS versions. So if your application not only supports iOS 7, use it when you design your interface.
Let's talk about the Autolayout feature now. Autolayout is not something new in iOS 7, but in Xcode 5 it was redesigned from the ground up, giving developers the ultimate freedom to set and change constraints. That means that you can set any constraints you want, they way you want (even delete them) without an automatic intervention from Xcode. It will warn you if there are any missing or constraints that do not match, but it will let you do whatever you want without limitations.
Contradictory to previous versions of Xcode, in Version 5 a new subview was added to a view that doesn't include any constraints. This lets developers decide what constraints they need. Also, new options for setting constraints are available at the bottom-right frame of the Interface Builder (alternatively, in the Editor menu), which you can use to either set custom values and certain constraints or to allow Xcode to automatically setup the layout for you.
When you set constraints and Xcode detects that any ambiguity or issues exist, it colors the constraint lines in red instead of blue. They stay that way until all issues regarding the problematic subview are resolved. Here is an example where constraints are missing and Xcode warns us using the line color:
Warnings or errors about missing constraints or misplaced views are also shown by Xcode in the Document Outline, next to the view that inconsistencies exist. To examine what the problem is, simply click on the little yellow or red arrow that appears right next to the view, and a list of all the layout issues appears. The most easy way to fix everything is to click on each warning or error sign that appears, and through a list of suggested and possible solutions that Xcode provides, to select the most appropriate one according to your needs. The other way is to keep playing around with the constraints of the view or the views that cause the issues, until you manage to silence all warnings or errors.
As a piece of advice, I recommend that when you use the Update Constraints, Add Missing Constraints, or Reset to Suggested Constraints options to let Xcode automatically setup all the constraints you don't need to manually touch. Those options can be found either in the menu Editor > Resolve Auto Layout Issues or in the respective button that appears at the bottom-right side of the drawing area of the Interface Builder.
4. Asset Catalog
Asset Catalog is another new feature of Xcode 5 with dual purpose:
- To put all of your images together and let you keep them in one place.
- To relieve you of the need to remember all naming conventions required for app icons and launch images.
With Asset Catalog you don't have to remember the rules that applies to app icon or launch image naming. Assets with these kind of images can now have any name you like. Xcode accepts everything and automatically gives the proper name to them. Also, Asset Catalog consists of the best place to have your images together and to access all of them at once when it's necessary.
Asset Catalog is represented as a group with a blue folder icon among the list of your groups and files on the Project Navigator.
Note: If you create a new iOS 7 application project on Xcode, the Asset Catalog is in its place. However, if you move an existing project to iOS 7, then you need to create the Asset Catalog on your own. This is easy to do. In the General tab of your project, inside the App Icons section there is a button with title Use Asset Catalog. Click on it and the catalog forms.
If you click on the Asset Catalog icon on the Project Navigator, you'll see a series of empty slots waiting to accept the icons of the application. The Asset Catalog feature displays slots for all the target devices you set in your project. To add an app icon, you can simply drag and drop one from a directory on your computer to the appropriate slot. The same rule applies to the launch images too, where there are empty slots for all the devices and supported orientation you have in the project options. Once again I'll remind you that app icons and launch images can have any name you like; Xcode sets the appropriate ones automatically.
Any other images you want to add to an application can be easily put in the Asset Catalog, simply by dragging and dropping them in there. If there are already images in your project, here is the quickest way to move them from the Project Navigator into the Asset Catalog:
- Click on the Plus button that resides at the bottom-left corner on the left pane of the Asset Catalog.
- Click on the Import from project... option.
- Select the images you want on the window that shows up (if they are not selected already) and then click on the Import button.
Once the images are imported or added directly from your computer, you'll notice that inside the Asset Catalog there is one record for every image. If you click on any of them, you'll see that there are two slots associated with it. The first accepts the normal resolution image (1x) and the second accepts the retina display resolution image (2x).
Debugging on Xcode 5 offers a new experience to developers, as new features were added to the debugger. Tracking down variable values is easier. Previewing images, documents, and other objects is now just a matter of a single click, and great flexibility occurs when working with breakpoints. The debugger on Xcode 5 is based on the LLDB engine, which totally replaced the gdb engine. LLDB is more powerful and underlies all the new debugging features. So, let's take a look at the improvements.
The first great visual improvement exists on data tips. A data tip displays a variable's value during debugging, when the mouse pointer hovers over the variable. For simple data type variables, the data tip shows simply the value, just like in the following figure:
Regarding objects, data tips provide summary information for every object that hovers, but the biggest change is that more than text can be displayed now. Images, text documents, bezier curves, and more can be previewed on Xcode during debugging, so it's easier for developers to examine their objects and be assured about their contents. The data and graphic combination gives super flexibility when trying to find bugs, as both single variables and objects can be previewed at once. This is a great feature that reduces debugging time! Take a look now at the next image to see an example:
Next to each object that you can preview, just like in the previous image, an eye and an info buttons are displayed. You can preview (Quick Look functionality) the selected object by clicking on the eye button. Also you can see detailed information by clicking on the info button.
Apart from the data tips, breakpoints were improved as well. If you previously debugged a project, then you know that breakpoints can be set simply by clicking at the left side of the lines where you want them to be. Breakpoints can be enabled or disabled either by clicking on each one of them, or by using the breakpoints button. With Xcode 5 this doesn't exist any more on the top toolbar, but it's been moved to the debug toolbar at the bottom of the Xcode window. Using this button, you can toggle all breakpoints at once. Enabled breakpoints are displayed in blue, and disabled breakpoints are displayed in grey. To completely remove a break point, right click on it and select the Delete Breakpoint option.
On Xcode 5 breakpoints conditionally work. Furthermore, actions attach to them when a condition is satisfied. That way, developers don't have to set and unset breakpoints all the time, they just need to add conditions or select breakpoints to ignore for certain times and focus more on debugging. The provided actions that are available and can attach to a break point when a condition is met are listed below:
- Capture OpenGL ES frame
- Debugger Command
- Log Message
- Shell Command
More than one action can be set to breakpoints when conditions are satisfied. For example, you may log a message and at the same time play a sound that notifies you on the break points condition. Here is an example:
So, when using these new features, debugging becomes much easier. Buggy code can be tracked down faster and wasted time can now be saved and given to other tasks. In combination with the next brand new feature, debugging becomes a whole new experience!
6. Debug Gauges
A long time ago, when system resources (such as CPU and memory) were limited in a computer system, programmers had to spend a great deal of time making sure their programs work using as few resources as possible. That was a hard task, but it was the best way to ensure that no memory leaks existed or pointless CPU usage occurred. Today, with the new powerful and fast sub-systems that devices use, it's quite common for developers to ignore any leaks their applications cause, because they simply see their apps running fairly fast on modern devices. However resources are always limited, and by ignoring them, a potential application crash would definitely lead to a bad user experience.
Here is where Debug Gauges make their debut, coming as auxiliary debugging tools that let developers track down unwanted usage of system resources. Actually, Debug Gauges consist of a lightweight version of some Instruments tools, they are embedded into Xcode (no need to open in new window like Instruments) and, above all, they can run all the time along with the application during debugging. Gauges allow you to observe the following resources:
- OpenGL ES
Debug Gauges are accessible through the Debug Navigator, and they appear when an application runs. The big, graphic elements represent every resource that's watched, so it's simple to use them.
Of course, for a thorough and detailed profiling of your application, you can use Instruments any time you need. A button which you can click to open Instruments at once is always at the top-right side of the gauges window. When an app runs, you can click from gauge to gauge and watch all measurements taking place in real time. If you notice any abnormal behavior, then you may suspect that something is wrong in your code and needs to be fixed.
You are encouraged to use these tools as much as possible in order to notice and fix bugs.
7. Testing and Bots
Testing is an important part in the development process, and special care should always be given to it. Thankfully with Xcode 5 testing applications is a lot easier. Particularly with the new Test Navigator. Test Navigator can be found on the navigator pane, and it lists all tests that you create.
Through navigator, tests can easily be edited, ran, or deleted. Next to each test its current state is displayed, whether it has succeeded, failed, or not yet ran. Great flexibility was also given to the way that tests run. So you can initiate a single test, select a bunch of them, or let them run simultaneously.
A new unit test framework was introduced on Xcode 5, the XCTest, which exists in parallel with the previous framework, the OCUnit. However, the first one will dominate the future. Therefore, it is recommended to use the XCTest in your projects. By default, new projects use this framework. Also, in Xcode 5, the tested code and the test itself can be placed side by side using the Assistant Editor. That way, you can watch both the code and test details while it's running.
Bots are a completely new feature on Xcode 5. A bot is a service, which repeatedly does a series of tasks, such as building, testing, analyzing, archiving, and signing an application (tasks that traditionally would be done manually by developers). These series of tasks are called integration process. With bots, the integration process is automatic and can continue for as long as needed.
What makes bots special is that instead of running tests on the local computer, they can move those tests into a server and perform the integration process there. That way, computers used for development are not charged with test execution duties, and moreover, test results can be shared to all the developing team at once. In addition, bots can run multiple tests for multiple applications on devices attached to the server.
Bots can be configured to work in two ways. According to the first way, bots perform the integration process on code that is taken from a source control system like Git. When new changes are available, bots start the integration process over and keep going until the app has been completed and no more testing is needed. These bots are called continuous. The second way is that the integration process repeats as soon as possible. Bots that are configured to work in this manner are called nightly or periodic bots, and they sign and archive the app on every reproduction step, with one more result, to keep track of history during time.
For developing teams, bots consist of a great way to run tests and share the results. Also, through the extensive testing that is performed due to the continuous integration process, finding bugs and errors is much easier, while at the same time due to the nature of testings, bugs can be isolated or located in regard to specific hardware or other settings. Bots are a tremendous leap for Xcode!
8. Compiler Improvements
Until now, all new Xcode 5 features were presented one by one, but this tutorial wouldn't be complete if no reference was made to improvements under the surface. So, let's have a brief tour of the major changes and additions made to the compiler.
First of all, until version 4.6, Xcode supported two compilers: Apple LLVM and LLVM-GCC 4.2. On Xcode 5, the latter is gone, and only Apple LLVM remains, empowered with new features that make it even more useful.
LLVM compiler introduces the logic of Modules. But before explaining what a module is, let's see in short the problem that it counterattacks. When a compiler parses a file, it doesn't parse just the code existing into that file, but it also does that for all the header files that are imported (using the #import command) to the file. This process works recursively, and all files imported are finally parsed until the last line of code. This means that instead of parsing only the lines of code existing in the initial file, thousands more lines have to be parsed due to the continuing imports. This is not a new problem and modules came to provide a solution. A module is a simple, optimized database which keeps the parsed code of a single system header file (for example UIKit.h, Webkit.h, etc.) within. Compiler builds a set of such modules, and when it's time to parse our own file, it doesn't have to parse the header files again. Taking into account that the same files are imported again and again multiple times, it becomes obvious that modules give great performance on compile. Moreover, Xcode itself uses modules to achieve some of its advanced features, such as syntax, highlighting, and auto-completion.
Another new feature of the compiler is the Auto-vectorizer, which was added clearly for performance reasons. Its job is to analyze code and find segments, where two or more operations (up to 32) can run at the same time. That's because the new, modern CPUs offer great vector features for virtual multi-threading, and Autovectorizer takes advantage of this. In applications where intensive calculations are required, this feature can truly boost performance, depending on the type of calculations.
The compiler can now read the code documentation and produce help tips, just like it does with the built-in documentation. So, the more you document your code, the more detailed help is provided for it by Xcode. This features makes much more sense in case you use custom code from other developers. Here is an example of the help provided for custom code documentation:
Finally, regarding the compiler, there are a couple of points about the static analyzer feature that should be mentioned. Static analyzer was improved and it now can do deeper code analysis to help you find more bugs. But what actually makes developers happy is the new feature that allows the user to perform static analysis on a single file, or even build a single file, instead of analyzing the whole project over and over again. That's a great improvement and lets you focus just on the file you are working on.
Xcode 5 is a developer's dream because it helps the user build a bug-free and extensively tested application. It's not hard to adopt all the changes, and once you do it you'll love them. If you haven't moved to the latest version yet, then I strongly encourage you to do so and enjoy all of Xcode's new features. As far as Xcode 5 is concerned, keep exploring and you will be rewarded!