Know Your Tools to Build Better iOS Apps
One technique for new iOS developers who want to up their game is just to get more familiar with the tools of the trade. By understanding what tools are available and how to use those tools, developers can write code that is shorter, simpler, faster, and easier to maintain.
In this article, we’ll take a brief look at some of the most commonly used tools in iOS development, and some specific things that a new developer can learn to get a lot of leverage.
Xcode is the integrated development environment that Apple provides for iOS development. A developer will use Xcode to set up a new project, build out a user interface in Storyboard, build and edit class files, run and debug a project, resolve performance issues with Instruments, and package and submit apps to the App Store.
- Storyboards: There are two critical techniques to know about storyboards. First is the ability to quickly build out a user interface. With a storyboard, a skilled developer can build out the user interface for an app quickly, including navigation and basic interactions with no code. Second are Size Classes and Auto Layout. With effective use of these, a skilled developer can handle different device sizes and orientations all in the Storyboard, again with little to no code. Size Classes can even support completely different layouts on different devices. Reducing the amount of code in the project reduces the potential for bugs and long-term technical debt. More details about Auto Layout and Size Classes are available at the Apple Developer website, and in WWDC videos 2014: Session 216, “Building Adaptive Apps with UIKit“ and Session 411, “What’s New in Interface Builder.”
- Debugging and Basic Profiling: All code has bugs, and some bugs are more difficult to diagnose and resolve than others. Xcode has a powerful debugger (lldb) built in that can be a great aid in diagnosing and resolving bugs. The debugger can display previews of objects in code, including images (hint: implement the description method in your objects to display extra detail). It can set breakpoints with many different and useful variations: logging breakpoints to print information to the console without adding log statements to your code; conditional breakpoints to stop only when specified criteria are met; action breakpoints can run a script or play a sound when a breakpoint is hit; exception breakpoints stop when Objective-C exceptions are thrown, making it easier to trace crashes when they happen; and symbolic breakpoints to stop action when a method name is called, even if the source for that method is not available. In addition to the debugger, Xcode provides a basic profiler in the debugging view to show memory and CPU utilization while debugging an app. This view can help a developer identify performance problems in the app, which can then be investigated in detail using Instruments. Chapter 26, “Debugging and Instruments” in Mastering iOS Frameworks provides an introduction to all these topics.
Apple has many years of investment in the iOS (Cocoa Touch) SDK. iOS has its roots in OS X, the Macintosh operating system, and even shares some code with it in the Foundation classes. From basic technologies like buttons and labels to show on a view, to more advanced technologies like collection views, to relatively obscure technologies like Core Image or Handoff, the SDK contains a wealth of functionality and power available for the developer to discover and leverage.
- Getting off the main queue: One of the biggest leaps a developer can make is being able to use background queues effectively to reduce the amount of load on the main queue. This requires understanding the techniques available to execute code off the main queue, knowing how to coordinate any background activity with updating the user interface, and preventing some of the pitfalls of working in background queues like deadlocks, race conditions, or inconsistent user interface updates. Working off the main queue requires knowledge of NSObject methods, NSOperations and NSOperationQueues, and Grand Central Dispatch techniques. These techniques are described in detail in Chapter 18, “Grand Central Dispatch for Performance” in Mastering iOS Frameworks.
- Networking: Many apps need to use networking to communicate with the outside world. Apple has updated the internal networking stack as of iOS 7 (NSURLSession and NSURLSessionTask). With the new tools, Apple provides lots of powerful networking capabilities. When combined with NSJSONSerialization for JSON parsing and NSURLCache for request and image caching, a developer can do everything that popular third-party libraries do and still know what is going on under the hood. Some of these techniques are used in the sample apps used throughout Mastering iOS Frameworks, and Chapter 9, “Working with and Parsing JSON” describes how to deal with JSON specifically.
- Frameworks for specific purposes: Apple has a number of frameworks developed to support specific purposes, like using music libraries, interacting with the address book, storing and interacting with data using Core Data, using HomeKit or HealthKit, or adding Game Center to an app. Each of these frameworks requires specific knowledge to apply effectively in an app. Mastering iOS Frameworks provides many chapters that describe how to get up to speed on specific frameworks, with sample apps that illustrate the concepts and how to implement them.
Languages (Objective-C and Swift)
iOS apps can be built using Objective-C, Swift, or a combination of the two. Knowledge of Objective-C is still important, because the vast majority of the iOS SDK is written in Objective-C. Swift provides excellent interoperability with Objective-C code, and has a number of developer-friendly features like Playgrounds that make it a great option for developing faster and better.
- Objective-C: With the dynamic nature of Objective-C, there are a number of techniques that are obscure but can be very valuable. For example, an Objective-C category allows a developer to add a method to an existing class, without having the source code for the original class. This can be very useful for adding utility methods to standard classes like NSString or UIImage. Using Key-Value concepts can be very powerful, both for observing and responding to changes in objects, and for performing bulk operations. Let’s say I have an array of people, and I need a list of names to show in a picker. Instead of iterating over the array, getting the name for each instance, and adding to another array, simply performing [myArrayOfPeople valueForKey:@“name”] will provide the list of names in one call. Finally, using blocks can be a very powerful technique in many use cases. Blocks are used extensively in the iOS SDK for things like completion handlers and enumeration handlers. Because blocks can contain both code and data, and are objects that can be stored in arrays or passed as method parameters, they provide a great deal of power and versatility.
- Swift: Xcode includes a tool for learning, practicing, and developing Swift in an interactive way called a Playground. A Swift Playground is an abbreviated project with special settings to allow the compiler to evaluate and execute Swift code as you type, with a window to show results in real time. This lets the developer experiment and learn quickly because feedback is immediate. Swift is a strongly typed language that offers modern syntax and features. It supports pass by value structs and pass by reference classes, functions as types that can be passed around like objects, and optionals that explicitly handle cases where nil values can occur. All these features can be applied to write clean, tight, and comprehensible code. Swift was developed with specific support for interoperating with Objective-C. Many Objective-C classes are automatically converted to the equivalent Swift type, and some Swift types can be converted to Objective-C. Because the great majority of the iOS SDK is still in Objective-C, it is important to know how to interact with the Objective-C SDK from Swift. For a good introduction to Swift, check out Swift for the Really Impatient by Matt Henderson and Dave Wood.
Git (Version Control)
Git is a distributed version control system, originally written by Linus Torvalds. It is natively supported by Xcode, and is the default version control system for new projects created in Xcode. Using version control is absolutely required in a team project. For an indie developer, following the discipline of version control can avoid a lot of common problems, and make life easier when creating new versions and dot fixes, and integrating new features. Using remote repositories provides a great way to keep a backup of a project in addition to syncing changes between people or machines.
- Branches: Using a branch is a good technique for adding a feature or working on a fix. A branch can be created from the current working directory, allowing any work to be isolated in one place. When work is complete, it can be discarded with no effect on the original code, or can be merged into the original code. Branches can be compared visually to see what differences there are between them.
- Viewing history: When a feature unexpectedly breaks, looking at commit history in git can help a developer figure out what went wrong. History can be viewed by file across commits, and by commit across files. Git can also show who made changes in a source file (called blame) by line, making it easy to figure out who to ask if a line looks incorrect or does not make sense.
- Remote repositories: A remote repository can be used both as a backup for a project’s source code and as a way to integrate changes from multiple developers. Services like GitHub and Bitbucket provide a quick and easy way to set up a remote repository for a project. Once the remote repository has been set up, local changes can be pushed to it, and remote changes can be pulled from it. Branches can be pushed and pulled from the remote repository independently. In GitHub, for example, a developer can create a pull request from one branch into another branch, and then request another developer to review it. This is a good technique to get extra eyes on code, discuss any potential problems, and ensure consistency in how problems are approached in a project. Once any potential changes have been addressed in the branch locally and pushed to the remote repository, they can then be merged on the remote repository directly.
More information about Git is available at http://git-scm.com, and information about using Git in Xcode is available at http://www.cimgf.com/2013/12/10/using-git-in-xcode.
There are lots of opportunities for a new iOS developer to improve their skills, especially by getting more familiar with the tools commonly used for iOS development. From writing user interfaces that support multiple device sizes and orientations with no code in Xcode Storyboards, to adding utility methods to existing classes using Objective-C categories, to specific frameworks in the iOS SDK, to elegant and efficient code management using Git, a developer can find many techniques to reduce the amount of code written, improve the quality of that code, and make building apps more fun and rewarding. Enjoy exploring and learning!