Using Zumero in your Swift project
We’ve received an email or two asking how to use Zumero sync from Swift code. If you’ve used other Objective-C frameworks in your Swift projects, the steps are just what you’d expect. If you haven’t, don’t worry - it’s not that bad.
Create a Swift-based project, as usual.
Now add the ZumeroSync framework - either directly from the Zumero SDK, or – my preferred way – via Cocoapods.
$ cat >> Podfile
pod 'ZumeroSync'
$ pod install
Analyzing dependencies
Downloading dependencies
Installing ZumeroSync (2.0.0.2589)
Installing sqlite3 (3.8.8.3)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `swiftnzum.xcworkspace` for this project from now on.
We’ll now open the workspace as instructed, and it looks like we’re all set:
Except that Swift has no idea what we’re talking about:
To let Swift code know about the Zumero classes, we need a bridging header. I followed Mark Petherbridge’s instructions in How to create an Objective-C Bridging header, like so:
Add a new Objective-C file. Call it what you like:
Yes, we do want to create a bridging header:
Add a ZumeroSync
import to that header:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import <ZumeroSync/ZumeroSync.h>
We can delete the “dummy” Objective-C file (optional, but it serves no purpose here):
And voilà, Swift has heard of Zumero:
As for the actual usage, it’s just a Swift-y version of the Objective-C call:
var syncError: NSError?
let ok = ZumeroSync.Sync("foo", cipherKey: nil,
serverUrl: "http://example.com/", remote: "dbfilename",
authScheme: nil, user: nil, password: nil,
error: &syncError)