Setting up YapDatabase with Swift
Incase if anyone couldn't figure it out, here's how I got YapDatabase setup on a new Swift project.
- In Xcode, create a new Swift project. The name of the project will be referred to as appname from here on out. Substitute appname with your project name. (Any project template will do. Make sure to select the Swift language)
- Add the YapDatabase pod to your new project. (You should already know how to do this)
a. In terminal, navigate to your project directory.
b. Type "pod init"
c. Modify the platform. (i.e. platform: ios "7.0")
d. Add "pod 'YapDatabase', '~> 2.4' to your project
target "appname" do pod 'YapDatabase', '~> 2.4' ende. In terminal, type "pod install" f. Open up appname.xcworkspace - Add a file called: appname-Bridging-Header.h to your Swift project. Add the following import:
# import <YapDatabase/YapDatabase.h>
1. Under "Build Settings" > "Swift Compiler - Code Generation", look for the "Objective-C Bridging Header" property. Type in: appname/appname-Bridging-Header.h
2. Now you can test out the "Hello World" code:
var paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)
var baseDir = paths.count > 0 ? paths[0] as String : NSTemporaryDirectory() as String
var database = YapDatabase(path: baseDir.stringByAppendingPathComponent("YapDatabase.sqlite"))
var connection = database.newConnection()
connection.readWriteWithBlock({
(transaction: YapDatabaseReadWriteTransaction!) in
transaction.setObject("Hello", forKey: "World", inCollection: "example1")
})
connection.readWithBlock({
(transaction: YapDatabaseReadTransaction!) in
var message : String! = transaction.objectForKey("World", inCollection: "example1") as String!
NSLog("%@", message)
})
Hopefully this will help someone out wanting to try out Swift with YapDatabase! Let me know if you want me to clean this up and add it to the Wiki.
Very helpful for initial setup. Thank you.
I'm having a problem setting up a YapDatabaseView. From my view controller's viewDidLoad: method I try to get a view instance from the following Swift func. The compiler error is, "Could not find overload for 'init' that accepts the supplied arguments". I've tried every init syntax combination of labels I could think of with no luck, although as I understand from the WWDC 2014 "Integrating Swift with Objective-C" video, the following should work.
Note that in order to resolve needed symbols I had to add a number of #import statements to ${PODS_ROOT}/Headers/YapDatabase/YapDatabase.h. I am able to initialize stored YapDatabase property instance in the view controller initializer.
func albumDbView() -> YapDatabaseView {
var groupingBlockType: YapDatabaseViewBlockType = YapDatabaseViewBlockTypeWithObject
var groupingBlock = {
(collection: String!, key: String!, object: AnyObject!) -> String? in
if object is MWS_Album {
return MWS_albumsKey
}
return nil; // exclude from view
} as YapDatabaseViewGroupingWithObjectBlock
var sortingBlockType: YapDatabaseViewBlockType = YapDatabaseViewBlockTypeWithObject
var sortingBlock = {
(group: String!, collection1: String!, key1: String!, obj1: AnyObject!,
collection2: String!, key2: String!, obj2: AnyObject!) -> NSComparisonResult in
if obj1 is MWS_Album && obj2 is MWS_Album {
var title1 = obj1.title as NSString
var title2 = obj2.title as NSString
return title1.caseInsensitiveCompare(title2)
}
return NSComparisonResult.OrderedSame
} as YapDatabaseViewSortingWithObjectBlock
var yapDbView = YapDatabaseView(groupingBlock: groupingBlock, groupingBlockType: groupingBlockType, sortingBlock: sortingBlock, sortingBlockType: sortingBlockType)
return yapDbView
}
@seeker12 I just pushed some changes that should make it much easier to use YapDatabaseView in Swift.
I am using YapDatabase for storing my objects. I Need how to store multiple entries in a table.
For example : I need to save all students information in Student table. So how can I do that with YapDatabase using Swift.
var paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)
let baseDir:String = paths.count > 0 ? paths[0] as String : NSTemporaryDirectory() as String
let path = baseDir.stringByAppendingString("Database.sqlite")
yapDB = YapDatabase(path: path)
yapDataBaseConection = yapDB?.newConnection()
yapDataBaseConection?.asyncReadWriteWithBlock({ transaction in
}, completionBlock: {
});