Perfect, Swift

Using PerfectLib with macOS apps


While developing a macOS app that needed a lot of file & directory access, as well as a number of curl and command line operations, it struck me that integrating the Perfect framework in my app was a no-brainer idea… this is how.

Some background

In June of this 2016 I joined the team developing Perfect, a server side Swift framework. In doing so I embarked on a rapid learning curve with Swift. I’ve a bunch of experience using Go (golang) and have been pleasantly surprised at how accessible Swift has become.

Why would I include a server side framework in a macOS app?

Yes, I know, Foundation includes the filemanager and it’s possible to use that for file system operations.

Yes, I know, you can also create routines in Swift that execute command line directives.

But, with the PerfectLib embedded you don’t need to recreate the wheel. Reading, writing files is more efficient in your code. Parsing JSON is a snap. Cloning a repo from GitHub is a piece of cake.

Steps

0. Install Xcode 9

If you don’t already have Xcode 9 installed… get it, install it. You can get it directly from the App Store, and it’s free.

1. Clone the Perfect repo, build and generate an Xcode project

In the terminal, navigate to a place in your hard drive where you’d like to keep the library, execute:

git clone https://github.com/PerfectlySoft/PerfectLib.git
cd PerfectLib
swift build
swift package generate-xcodeproj

This checks out the latest PerfectLib, builds it, and generates an Xcode project.

2. Create an Xcode Workspace including your macOS project

If you haven’t already created an Xcode Workspace for your project, do so. Your project should be inside it.

3. Add Perfect to your Workspace

Drag the Xcode project we created in step 1 into the Workspace. You’ll see it beside your macOS project.

4. Build PerfectLib in Xcode

In step 1 we build PerfectLib with the terminal. While that verifies that all is well, it doesn’t provide Xcode with what it needs.

Select the PerfectLib scheme in Xcode, and press cmd-B to build it.

5. Include PerfectLib as a “Linked Framework”

6. Import PerfectLib in your code and use it

In your code, now you can use PerfectLib:

import PerfectLib

And that’s all there is to it.

I am hoping that once Xcode gains proper support for the Swift Package Manager this will all become much easier, but for now even the “hard” way should be achievable for most people.