Skip to content

Hacking Clover with Apple’s new Swift programming language

June 8, 2014

OK, it’s been a while since I’ve posted anything here on Fun in Space, but now I have something interesting to show.

I experimented a bit with Apple’s new Swift programming language over the weekend and decided to try out accessing Clover’s web services (i.e. the stuff I work on during the day mostly) using Swift as a scripting language, more or less.  Based on this tweet from Chris Lattner, I decided to try out a simple script for accessing point-of-sale order information via the Clover API.

I’m pleased to report that the the experiment was successful and I had fun doing it.  The results are posted on Github here.  A few notes:

  • I’m quite amazed to see how much info there is on Swift already on the Internet.  After being announced on Monday (6 days ago as of the time I’m writing this), virtually all of the questions I needed help answering were addressed in some way in either the documentation or the discussions on Stack Overflow and other places with respect to Swift.
  • I like the static typing and type inference, but accessing JSON data via NSJSONSerialization was still a bit of a pain. If I were to develop a full-fledged Swift application, I’d probably use some type of data binding approach as we do in Java with the Clover Android SDK.
  • I got a few hints from the Agent project, which provides a thin web request API on top of Swift.  I chose to use NSURLSession instead of NSURLConnection that Agent uses, and this proved to be very straightforward.
  • One part that was potentially quite tricky was that my script exited immediately before the NSURLSession tasks could be completed.  I had to dig up some old information from my brain (with some help from Stack Overflow and this Gist) and put in a manual NSRunLoop to prevent the script from exiting until my tasks were completed.
  • Passing in command line arguments to the script didn’t seem to work too well, so I relied on an environment variable (CLOVER_TOKEN) to pass in the authentication token to the script.

If anyone else accessing web services via Swift gives this a try and has any feedback, please let me know.

From → Uncategorized

One Comment
  1. steveseagraves permalink

    Nice job!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: