Swift and the ArcGIS Runtime SDK for iOS

Blog Post created by NFurness-esristaff Employee on Jul 21, 2014

Swift is a new language introduced by Apple to ease writing iOS and OS X applications. And at the Speedgeeking session during the Esri UC last week, I showed some examples of working with Swift and our Runtime SDKs. Here's my very-much-in-flux repo that formed the basis of that.


Swift and ArcGIS today

You can use Swift today with the 10.2.3 Runtime. It takes a tiny bit of configuration of your installed SDK.


The first and only step is to add a module definition file to your installation of the Runtime and is described in this Gist.


framework module ArcGIS {
  umbrella header "ArcGIS.h"

  export *
  module * { export * }


Credit goes to Eric Ito who sat down with the Apple guys at WWDC. It took them all of 20 seconds to work out as I took notes looking over their shoulders. I know this would have taken me an age to figure out.


Note also, the above is only necessary in a pure Swift project. An Objective-C project that you add Swift to doesn't even need that.


Supporting Swift

Quite a few questions I got at the User's Conference last week revolved around the ArcGIS Runtime SDKs for iOS/OS X and Esri's support of Swift. The truth is, Apple have done such a good job in building bridges between Objective-C and Swift that we don't actually need to do anything to let you get full use of the our SDK out of the box. And the above module definition will be included with our next SDK release, making it even simpler.


import UIKit
import ArcGIS

var tileUrl = ""
var nycZipsUrl = ""
var zipCodeLayerName = "Zipcodes"

class ViewController: UIViewController, AGSMapViewTouchDelegate {
    @IBOutlet var mapView:AGSMapView
    override func viewDidLoad() {        
        // Do any additional setup after loading the view, typically from a nib.
        var basemap = AGSTiledMapServiceLayer(URL: NSURL(string: tileUrl))
        mapView.addMapLayer(basemap, withName: "Basemap")
        var zipcodes = AGSFeatureLayer(URL: NSURL(string: nycZipsUrl), mode: AGSFeatureLayerModeOnDemand)
        mapView.addMapLayer(zipcodes, withName: zipCodeLayerName)
        mapView.touchDelegate = self

    func mapView(mapView: AGSMapView, didClickAtPoint screen:CGPoint, mapPoint mappoint:AGSPoint, features touchedFeatures:Dictionary<String,[AGSFeature]>) {
        if touchedFeatures[zipCodeLayerName]?.count > 0 {
            println("\(touchedFeatures[zipCodeLayerName]![0]) was a zipcode!")


That doesn't mean we aren't thinking about ways to make things more "Swifty" when the time comes, but I wouldn't imagine it'll be a priority until at least such a time as the language settles down. The Runtime team have a ton more exciting things to bring you first.


One other thing that was news to some folks in San Diego was that Swift apps can target iOS7 and Mavericks just fine. No need for iOS8 or Yosemite, but remember: you can't even release anything written in Swift yet, and only Apple will tell you when that'll be ("the fall" is all the community knows so far).


Working with Swift

Swift is currently in beta. Unusually for Apple, the typical NDAs barely apply and many people are actively discussing it and writing about it publicly. You can find entire sites dedicated to it, and StackOverflow threads abound. Apple's own engineers seem actively involved in discussions around the web.


My own experience has been that it's a little touch-and-go at the moment. With each beta release, certain aspects of the language break as others are fixed, so prepare yourself for some frustration until initial release around iOS8 "in the fall". To be clear, these breaks and fixes haven't affected using the Runtime SDK, they've affected the way the language compiles and how the static analyzer tells you things are wrong, when perhaps they're not . I describe it as Apple untying their shoelaces to tie them up properly.


One area I'd love to see Apple improve is being able to use third party frameworks (e.g. ours!) in an interactive Swift Playground. Right now, you simple cannot. I've had no luck with our framework in the Swift REPL either (probably by design, and for the same reasons, but I'm going to keep trying).


I'd love to hear your experiences of playing with Swift and the ArcGIS Runtimes.