Skip to content

Setting up YapDatabase with Swift #85

@zenkimoto

Description

@zenkimoto

Incase if anyone couldn't figure it out, here's how I got YapDatabase setup on a new Swift project.

  1. 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)
  2. 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'
    end

    e. In terminal, type "pod install"
    f. Open up appname.xcworkspace
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions