Overview
Parse is a database service for mobile owned by Facebook. This post will show you how easy it is to create a persistent cloud without writing a single line of code for the backend. You can get more information about Parse here.
Content
You first need the bridging header. So create an objective-c .m
file of any name. You will be prompted to add a bridge header, accept. Now you can remove the initial objective-c file that you created (we only needed the bridge header).
Add the following import into the briding header so that you can use those them in swift.
1
2
#import <Parse/Parse.h>
// or #import <ParseOSX/ParseOSX.h>
Now that you have the library, you can use it in Swift. To add an object and save it.
1
2
3
4
5
6
7
8
9
10
11
var honeyObject = PFObject(className: "Dogs")
honeyObject.setObject(2, forKey: "age")
honeyObject.setObject("Honey", forKey: "dogName")
honeyObject.saveInBackgroundWithBlock {
(success: Bool!, error: NSError!) -> Void in
if success {
println("Object created with id: \(honeyObject.objectId)")
} else {
// error
}
}
If you want to retrieve it, you do the following.
1
2
3
4
5
6
7
8
9
var query = PFQuery(className: "Dogs")
query.getObjectInBackgroundWithId(honeyObject.objectId) {
(honeyObjectAgain: PFObject!, error: NSError!) -> Void in
if !error {
println("We found a dog named \(honeyObjectAgain.objectForKey("dogName") as NSString))
} else {
// error
}
}