Tuesday, July 12, 2016

Swift Support

It's new, it's trendy, it's the latest thing: Swift support in WhirlyGlobe-Maply!

func createLayer(baseView: MaplyBaseViewController) -> (MaplyQuadImageTilesLayer) {
let precipTileSource = MaplyMultiplexTileSource(sources: tileSources)
// Create a precipitation layer that animates
let precipLayer = MaplyQuadImageTilesLayer(tileSource: precipTileSource!)
precipLayer?.imageDepth = UInt32(tileSources.count)
precipLayer?.animationPeriod = 1.0
precipLayer?.imageFormat = MaplyQuadImageFormat.ImageUByteRed
precipLayer?.numSimultaneousFetches = 4
precipLayer?.handleEdges = false
precipLayer?.coverPoles = false
precipLayer?.shaderProgramName = WeatherShader.setupWeatherShader(baseView)
precipLayer?.fade = 0.5
return precipLayer!
}
Swift has been in there since 2.4.1 (soon 2.5), but we haven't really pointed it out.

Why Swift?


I'm not the audience for Swift.  My favorite language is a mixture of C++ and Objective-C.  You get STL containers, blocks, and ARC.  It's awesome.  Why are you backing away?  Let me tell you more!

This is why I work with other humans.  José was quite interested in Swift and did the bulk of the work.

Swift Interfaces & Documentation


The main issue was Swift bindings for all the various methods.  Swift can interface to Objective-C just fine, but if you want to make it pleasant, it should look like this.

/** @brief Interpolate a new state between the given states A and B.
@details This does a simple interpolation (lat/lon, not great circle) between the two animation states.
*/
+ (nonnull WhirlyGlobeViewControllerAnimationState *)Interpolate:(double)t from:(WhirlyGlobeViewControllerAnimationState *__nonnull)stateA to:(WhirlyGlobeViewControllerAnimationState *__nonnull)stateB;
He did that everywhere and tested it out.  The result is a nice, pleasant Swift interface to the main library.  But we didn't stop there.

Feel the learning!



The tutorials have tabs for Objective-C and Swift!

Swift Tests


In the new AutoTester app (post forthcoming) José wrote a bunch of Swift test cases.  These have a pleasing rhythm to them.

func insertClusteredMarkers(compB : NSArray, theBaseView: MaplyBaseViewController) {
let size = CGSizeMake(32, 32)
let image = UIImage (named: "alcohol-shop-24@2x")
var markers = [MaplyScreenMarker]()
for object in compB {
let marker = MaplyScreenMarker()
marker.image = image
marker.loc = (object as! MaplyVectorObject).center()
marker.size = size
marker.userObject = object.userObject
markers.append(marker)
}
theBaseView.addScreenMarkers(markers, desc: [kMaplyClusterGroup: 0], mode: MaplyThreadMode.Current)
}

Swift-ly into the Future


We're getting user submitted github Issues in Swift now.  Not about Swift mind you, just in Swift.  So that seems to be working.

As Apple changes the language we'll keep up and I think a few new WhirlyGlobe-Maply apps may be written in Swift.

No comments:

Post a Comment