Unit Testing, or XCTesting as it’s called since Xcode 5, is a process that lets us test several aspects of our apps. For example, we can see if methods have return values we expect, and if not, flag this up to us. Since Xcode 7 and iOS 9 we can even test user interface events too.
Testing has come a long way in Xcode, but it’s also long been a mystery to me. I decided to explore it and make some notes so I wouldn’t forget.
Turns out that Unit Testing isn’t actually that difficult a concept to comprehend, but it’s usually presented in a way that only those with an IQ of 1000 or greater would understand it. Apple’s new document called Testing with Xcode has certainly helped me understand the basics, which I’ll discuss in this article. Continue reading
I really like the code completion feature in Xcode. It gives handy clues about the parameters of all methods and their parameters. But as soon as we hit return, the completion disappears. Sometimes I wish I could bring it back and re-read the descriptions.
Well I have great news: you can bring it back at any time simply by positioning your cursor on the method in question and hitting ESC as usual, or CTRL + SPACE. As soon as you’ve satisfied your curiosity, hit ESC to make it go away again.
Thanks to Rainer Schwarze and mndgs for this tip!
Do the debug messages appear too small for your tired eyes? Fret not, you can change those to something larger if you like:
Head over to Xcode – Preferences and find the Fonts and Colors section. Switch to the Console Tab and select an option you want to change (typically Executable Console Output and Debugger Console Output appear in the debugger).
Now click the tiny little T icon at the bottom and change the font to something more palatable. The default font is Menlo 11.
Alternatively you can switch to either of the Presentation presets, which will increase the font size for the debugger and your code font (good for screencasts).
There appears to be a bug in Xcode 7 that you may or may not encounter: when creating a new project with a Git repository, Xcode 7 may tall you that it “couldn’t communicate with a helper application”. The same bug may tell you that it “couldn’t commit files”.
This happened to me after upgrading from Xcode 6.x with which I’ve never had this problem.
Turns out the “helper application” is in fact Git: for some reason Xcode 7 is eager to associate you (the committer) with a name and an email address. Xcode even offers to access your Mac contacts upon first launch.
To fix it, all we need to do is launch our trusty Terminal app (the command line tool) and tell Xcode who we are, and those errors will be a thing of the past. The good news is that it only has to happen once. Here’s how to do it:
On the command line, type the following:
xcrun git config --global user.email email@example.com
xcrun git config --global user.name "Your Name Here"
You won’t receive any feedback if everything went well here. But next time you commit or create a new project those pesky error messages will be a thing of the past (until you encounter the next big Xcode quirk of 2015).
Kudos to Kenneth Vittetoe for this solution!
I’ve updated Xcode yesterday from version 6.4 to 7, and lo and behold, I couldn’t find those little triangles anymore that used to let me fold and unfold my code. You know, the ones that let you collapse a method so that code becomes more readable I really don’t like it when a simple upgrade punches me in the face like that.
Turns out that the Xcode upgrade had disabled this setting under Preferences – Text Editing. See the option that reads “Code Folding Ribbon”? It was un-checked after the upgrade, removing the little triangles from the gutter (where you set break points). Enabling it brings our code folding back (see screenshot above).
There are other methods of collapsing code of course: to collapse all methods, hit SHIFT + OPTION + CMD + LEFT CURSOR. Likewise, to open all methods, use SHIFT + OPTION + CMD + RIGHT CURSOR.
To open a single method, you can also click those little three dots in the yellow brackets.
And finally of course, we can use the menu as before. Head over to Editor – Code Folding for all these options and their respective keyboard shortcuts.
Did you know that the Interface Builder in Xcode lets you preview your layout without having to run the app in the simulator? It’s a well kept secret and very handy if your app doesn’t build (because the code is just not ready).
Here’s how to do it:
Select a storyboard or XIB file to bring up Interface Builder. You’ll see all your GUI components laid out as usual. Now switch to the Assistent Editor mode (two little circles at the top right; this used to be a butler type symbol prior to Xcode 6). In the jump bar you’ll see something like Automatic or Manual, followed by the corresponding file that has been preselected.
Click on this very word and see a menu that includes several options, among which you’ll find Preview. Select it and see a fly-out with all your storyboard files (there’s only one by default: Main.storyboard). Pick the one in question.
And hey presto: on the left side of your screen you see the adjustable Interface Builder, and on the right you’ll see what this layout would look like if you would run the app in the simulator.
Previewing different iOS devices
My Xcode defaults to the 4″ iPhone. It’s easy to add further configurations to this window so you can see side by side how various iPhone and iPad sizes display you components (if your monitor is big enough – you may not want to try this on an 11″ MacBook Air). See that little plus icon at the bottom left of the Preview window? Click it and choose another configuration. It’ll be added to the Preview window.
If you feel that you’re done with a particular configuration, select it in the Preview window (the outline of your dummy device turns blue) and hit the delete key. Begone, configuration!
If you have localised your app, you can even preview the various languages you’ve implemented. This is so much more convenient than changing the simulator’s regional settings every time you want to see if the German word for “Click here” fits on your small button (the translation would be something like “Bitte drücken Sie jetzt sofort diesen Knopf”).
Here’s how to preview localisations: at the bottom right of your Preview window you’ll see your current development language. Mine is English. Click it to bring up a menu with other languages this storyboard has been translated into.
If you don’t have a translation ready yet, you can make use of a “dummy” language that simply doubles doubles every every word word to emulate what longer words would look like.
Have fun with the Storyboard Preview!