Creating and using your own Xcode file templates

Working on an iOS or macOS project in Xcode you typically create classes with the same structure over and over again.

I use coordinators so I am creating new UIViewControllers, each time referencing RxSwift, having methods for setting up UI, bindings .. most of the time also containing a delegate for the coordinator.

Having to create files with the same structure over and over again manually is a waste of time, a much better solution is creating Xcode file templates for those files.

Xcode file templates

File template location

All the Xcode custom file templates are located in ~/Library/Developer/Xcode/Templates/File Templates and grouped into sections by their folder name. If you want Xcode to show a “Custom” section at the bottom of the new file dialog, just create a ~/Library/Developer/Xcode/Templates/File Templates/Custom folder.

File template structure

Each file template is a separate folder with a name ending in .xctemplate. If you want to create a simple “Swift Class” file template, you have to create a folder named Swift Class.xctemplate in ~/Library/Developer/Xcode/Templates/File Templates/Custom.

Each file template folder should contain at least 3 files:

  • TemplateInfo.plist - describing the template
  • TemplateIcon.png - icon shown in the Xcode new file dialog
  • ___FILEBASENAME___.swift - the actual template file

[Read More]

More readable XCode build output for CI

If you use Continuous Integration (CI) builds or build your app from the command line using xcodebuild you know that the output is not pretty and not very readable. Reading the build output is important when a CI build breaks, but it is not easy when it looks like this

Many iOS developers were not satisfied with this so the xcpretty project was created. Xcpretty is a fast and flexible formatter that turn the output from screnshot above to this neatly formatted output

[Read More]

iOS tip: Wireless debugging from XCode

One of the best XCode 9 features is the ability to deploy and debug iOS app on your device over WiFi, with no need to have the device connected to you computer by a cable. The only requirement is that the device runs iOS 11.

Setting it up is really easy. Connect the device using a cable like you normally do and go to Window | Devices and Simulators. You will see a new checkbox next to your iOS 11 devices called Connect via Network (see screenshot below), so check it. Now you can disconnect the cable and debug on your device over WiFi, the device has to be on the same network as your computer of course.

[Read More]

Formatting Swift code in XCode

When I started using XCode I was really surprised about the really poor implementation of its code formatting functionality. It kind of formats the alignment of the code but ignores unnecessary spaces and a lot of other things. Formatting the source code and keeping the style consistent is really important to me so I was looking for a solution. I found some linters like SwiftLint but I was interested in a tool that will actually format the source code for me on demand. I found SwiftFormat.

SwiftFormat

SwiftFormat is a code library and command-line tool for reformatting swift code. It applies a set of rules to the formatting and space around the code, leaving the meaning intact.

[Read More]