7 Tips for Web Developers and Software Engineers Learning iOS Development
Learning to write iOS apps and already know another programming language? Here are a few tips.
If you’re a beginner, “Should I learn Swift or Objective-C for iOS development?” may be more interesting.
1. Start by Exploring Xcode
Unlike many other development environments, an iOS application development environment is easy to configure. Here are the steps:
There’s ample command-line access if you want automation or need advanced workflows, but most work can be done within Xcode.
Xcode combines a powerful code text editor with graphical interfaces to a compiler, linter, debugger, user interface builder, documentation, and more. If you’re coming from a world where those are all separate, you’re in for a treat.
2. Decide Between Swift and Objective-C
Objective-C has a few advantages:
- Its syntax and usage is stable: tutorials and code samples from years ago work with only minor modifications.
- It’s very popular: most iOS app code bases are primarily written in Objective-C.
- Most iOS Developer job openings require Objective-C knowledge.
Swift has some strong advantages over Objective-C, though:
- A REPL, with an accompanying graphical interface called a “playground”. (A REPL is a console interface to a programming language, like irb, Pry, or node.)
- Swift has a great library from Apple of manuals, tutorials, and videos.
- It’s open-source, so you can see how it works and change it.
This video explores the differences a bit more. Both languages are powerful and full-featured, and both have full use of the iOS SDK.
If you’re not sure, I recommend starting with Objective-C, since that’s where the bulk of the job openings are at the moment. But you can’t go wrong either way.
3. Learn the Debugging Tools
Xcode’s debugging tools are among the industry’s best. Learn to use them for fast and efficient debugging and performance improvements. Some quick tips:
- Set a breakpoint by clicking on the line number. When the line is reached, execution will pause.
- Hover your mouse over a variable to see into its contents.
- Click a variable and press space bar to see a visual representation of its data (for example, if it’s an image or a color).
- Use LLDB to debug your app while execution is paused.
- Use Static Analysis to find logical flaws in your code.
Here are some resources to get started:
- Debugging in Xcode (2012 video; a bit dated but still helpful)
- Dancing in the Debugger — A Waltz with LLDB
- What’s New in LLDB (video)
4. Learn to Decipher Square Brackets
This method syntax is widely considered to be ugly:
[self.view insertSubview:button atIndex:2];
Confused? Don’t be:
- The first statement is the object that the method is called on (the receiver).
- The remaining statements are the method name interwoven with arguments:
view.insertSubview(button, atIndex: 2)
Learning to quickly interpret Objective-C square bracket notation is critical to your success as an iOS developer. Even if you’re working on apps in Swift, you’ll likely read sample code in Objective-C.
5. Don’t Be Intimidated by Objective-C’s Verbosity
Say you have a string like ‘2,4,5’ that you’d like to convert to an array. In a language like Ruby you’d use split:
Objective-C has a similar feature, but its syntax is much more verbose:
Want to turn that array back into a string? Objective-C doesn’t have join; it has:
[@[@"2", @"4", @"5"] componentsJoinedByString:@","];
Objective-C naming conventions are more verbose than those in many other languages. It’s tempting to think that these methods are more complicated — or harder — because they’re longer, but they’re not.
Just like with the previous tip, even if you plan to work solely in Swift, it’s important to understand Objective-C’s style so you can read and interpret it correctly.
6. Understand Object vs. Value Semantics
// ERROR: implicit conversion is disallowed
[mutableArray addObject:7];// Works great
[mutableArray addObject:[NSNumber numberWithInt:7]];// You can also use this shorthand
In Objective-C, object semantics are required. In Swift, you can use objects or values for most things, but values are strongly preferred. This is another strong difference between Swift and most web programming languages.
7. Understand Automatic Reference Counting
In ARC, objects stay in memory if there is at least one strong reference to it. For example:
self.label.text = @"Hello World";
In this example, the string object (@“Hello World”) will stay in memory because it has one strong reference (from label). Now consider this:
self.label = nil;
Two things will happen:
- The label object will be deallocated because self is no longer referencing it
- The string object will be deallocated because label no longer exists, and therefore is no longer referencing the string
Be careful not to create a strong reference cycle (also called a retain cycle). This is an error caused by a circular graph of objects pointing to each other. It wastes memory and can cause other negative side effects. Here’s an example:
Because all of these objects have at least one strong reference, none of them will ever be deallocated. To resolve this, switch one of the references from strong to weak:
With a weak reference, if the CarDealership instance gets deallocated, all of its cars will get deallocated as we’d expect.
Thanks for reading these tips. I hope you found a few of them helpful. Follow me on Twitter if you’d like to get in touch.
If you’re interested in learning more by pair programming with a senior iOS developer, check out Bloc’s iOS for Engineers course.