I started a new iOS project in Xcode with SwiftUI and I want to install swift-format
to format the code as I work on the app. Unfortunately, it seems harder then I anticipated.
The library: https://github.com/apple/swift-format
I added swift-format
package using Xcode, like this: File
-> Swift Packages
-> Add Package Dependency...
.
And I have no idea why the executable is disabled:
Anyways, after selecting the first two options (SwiftFormat
and SwiftFormatConfiguration
) I don’t know how to use this library in order to format the code…
My question is, how I can run this library so that it formats the files for me?
2
Answers
You can use XCFormat. It is Xcode extension that you will find in the App Store. I think it is free. Then, you can access it through the editor menu in Xcode. Of course, you can also create a shortcut and simply use your keyboard for added convenience.
I made it work via Quick Actions and shortcut to format current document in Xcode with
swift-format
.ln -s /path/to/your/binary/swift-format /usr/local/bin/swift-format
xcode-swift-format
) and add the following Apple script to the action:Set shortcut in System Preferences -> Keyboard -> Shortcuts -> Services for your quick action:
Add Xcode to System Preferences -> Security & Privacy -> Privaciy -> Accessibility (This step is needed so script can send Command+S keystroke to Xcode if you try to format unsaved document, it means it will save document before formatting)
your.swift
file in Xcode and hit the shortcut in my caseAlt+Command+=
… it takes a second or two for the doc to be formatted.