On our recent remote KHalfday, a few of us from the iOS community at Jayway took the opportunity to share our best tips and tricks for a more pleasant and efficient development workflow. This turned out to be a great collaborative session where we all learned a lot of new nifty things. In this post, I have summarized a few of our best tips and tricks from the session. Enjoy:)
- Use warnings instead of TODOs: A very common way to remember to fix something in your codebase at a later time is to write a comment like:
//TODO: Fix this later!An alternative approach is to instead use warnings. Just write #warning(“Fix this later) in your code. This line will generate a warning in Xcode and warnings are usually not very pleasant to have in a project and you should therefore be more eager to fix that thing you left and not wait in all eternity.
- Enable spell checking: Another hidden feature in Xcode is automatic spell checking. Just navigate to Edit -> Format -> Spelling and Grammar -> Check Spelling While Typing and turn it on! Spelling errors will now be marked with a red dotted line, just as in any other text editor. Now it is much easier to find those annoying spelling mistakes and avoid relentless bugs bad spelling can introduce.
- Show build times in Xcode: Want to see how long it took to build your project? Or maybe just show off your machine’s performance to a friend? Write this command in your console to toggle build times on and off in Xcode (change YES to NO to turn off): defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES
- Add and choose editors: Xcode 11 changed the way we work with multiple editors a bit. If you run Xcode on a larger monitor having multiple editors can be very helpful. A hidden feature is that you can add new editors both vertical and horizontal. Just press ⌥ and the button to add a new editor (in the top right corner) will change. It is also possible to open a new editor or choose one of the existing ones if you press ⇧⌥ while clicking on a file.
- Edit Shortcuts: In the Xcode Preference pane it is possible to edit and add key bindings for commands. A very useful shortcut is to move line(s) up and down. The default keybinding for this in Xcode is ⌥⌘[ or ⌥⌘]. If you are like me and use a Swedish keyboard layout, this command is actually not possible to execute with the default key binding. I have therefore changed the default binding to ⌥↑or ⌥↓ (which is the same shortcut as in VS Code). Just be aware that changing key bindings in Xcode can lead to conflicts.
- Use Emacs commands in Xcode: Did you know it is possible to use classic Emacs commands in the Xcode editor? Here are a number of nifty shortcuts to help you write and edit your code:
- Move cursor down: ^N
- Move cursor up: ^P
- Move cursor forward: ^F
- Move cursor backward: ^B
- Move cursor to beginning of line: ^A
- Move cursor to end of line: ^E
- Kill line: ^K
- Yank line: ^Y
- Delete: ^D
- Some more Xcode shortcuts: Two very handy shortcuts to navigate between files and methods in your code:
- Show files in the same folder: ^5
- Show methods in the file: ^6
- Environment Overrides: Xcode 11 makes it much easier to override common user interface settings directly from the debugger. Just look for this icon in the lower left row of debug buttons. Now it is much easier to for example toggle dark mode without having to switch between apps on the device.
SFSymbols: The release of Xcode 11 last year also introduced SF Symbols which are over 1500 free icons from Apple you can use in your app! The icons are designed to work together with the rest of Apple’s San Francisco (SF) typefaces and comes in nine different weights, from ultralight to black, to match the weight of the system fonts. Using them in a UIImage for example is really simple. Either just choose one from the dropdown menu in Storyboards or create one in Swift like this:
let image = UIImage(systemName: "square.and.arrow.up")
Just remember that SFSymbols are only available for iOS13 (or later) and can not be used in projects supporting older system versions. Apple also provides an app for macOS to browse all the new symbols. It can be downloaded from here.
QLPreviewController: A very common way to download and show pdf:s in your app is to use a WKWebView. An interesting and maybe not very common alternative is to use a QLPreviewController instead. Apple’s quick look framework actually lets you view a large number of files including pdf-, iWork-, Office-files, images and more! For more information about QLPreviewController check out Apple’s documentation here.