22

I added libsqlite3.0.dylib to my project, and then I tried to import using the following code:

import UIKit
import sqlite3

class Dataware: NSObject
{

}

But it's giving me this error:

No Such Module 'sqlite3'

Paaske
  • 4,345
  • 1
  • 21
  • 33
PREMKUMAR
  • 8,283
  • 8
  • 28
  • 51

4 Answers4

45

Add it to your Bridging-Header.h file:

#import <sqlite3.h>

This is the primary mechanism for importing any C-language libraries.

If you don't yet have a Bridging-Header.h file:

  1. Add a file Bridging-Header.h (or more typically (ProjectName)-Bridging-Header.h
  2. Go to the build settings tab for your project
  3. Find "Objective-C Bridging Header". The easiest way is to search for bridging.
  4. Enter the name and path for the file you created in step one. It's probably (ProjectName)/(ProjectName)-Bridging-Header.h
David Berry
  • 40,941
  • 12
  • 84
  • 95
7

when one want to add sqlite to framework target, module.map is needed
since sqlite is not mapped, and to do so just:
1. create file in your project 'module/module.map'
2. create the module from the umbrella header:

    module sqlite3 [system] {
       header "/Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.0.sdk/usr/include/sqlite3.h"
       link "sqlite3"
       export *
    }

*change the Xcode6-Beta5.app in the path to right one
3. connect the map file to you project, search for 'import paths' in 'Build Settings'and put the full path to the module file

Ben
  • 3,832
  • 1
  • 29
  • 30
  • this solution worked for me when tried to add sqlite to my swift framework – Ben Aug 21 '14 at 14:00
  • Hi @ben. Could you please elaborate your answer. I am not getting how to add a new module – Ratikanta Patra Nov 13 '14 at 12:59
  • 1
    Hi @RatikantaPatra, to add new module map, you need to: [1]create file called module.map [2]add the wanted module info (like in my answer) [3]in Xcode Go to Build Settings->set Swift Compiler->Search Paths->Import Paths to the directory that you put the module.map file in – Ben Dec 14 '14 at 14:52
  • Is it possible to change the absolute path in that file to something like $(SDKROOt)? Do you know if module.map files support something like that? – Stefan Arentz Mar 11 '15 at 02:49
  • 1
    @StefanArentz Module map files do not, but you can use an xcconfig file to reference different `MODULEMAP_FILE` paths per platform. See [my answer to another question](http://stackoverflow.com/a/29189873/86910), or my [SQLite.swift](https://github.com/stephencelis/SQLite.swift) framework (on the `swift-1-2` branch) for examples. – stephencelis Mar 22 '15 at 01:18
5

We need to import the header files for SQLite3 into the view controller so that the compiler can see the function and other definitions that make up the API.

There is no way to directly import the header file into Swift code, because the SQLite3 library is not packaged as a module.

The easiest way to deal with this is to add a bridging header to the project. Once you have a bridging header, you can add other header files to it, and those header files will be read by the Swift compiler. There are a couple of ways to add a bridging file. We’ll use the simpler of the two, which is to temporarily add an Objective-C class to the project. Let’s do that now.

File ➤ New ➤ File.... In the iOS section of the dialog, choose Cocoa Touch Class and press Next. Name the class Temporary, make it a subclass of NSObject, change the language to Objective-C, and press Next. In the next screen, press the Create button. When you do this, Xcode will pop up a window asking whether you want to create a bridging header. Press Yes. Now, in the Project Navigator, you’ll see the files for the new class (Temporary.m and Temporary.h) and the bridging header, which is called SQLite Persistence-Bridging-Header.h. Delete the Temporary.m and Temporary.h files—you don’t need them anymore. Select the bridging header to open it in the editor, and then add the following line to it:

#import < sqlite3.h>

Now that the compiler can see the SQLite3 library and header files, we can write some more code in ViewController.swift

That's it!

Rico Nguyen
  • 341
  • 6
  • 11
3

Hi Please follow these steps

In xcode 8.3.3 using swift 3

  1. Go to Build Phases tab
  2. Go to Link Binary with Libraries sub tab.

    (a) Click + button to add sqlite framework then search for sqlite then you can see libsqlite3.0.tbd and libsqlite3.tbd

    (b) Then select only libsqlite3.tbd(Don't add both because the compiler can not find sqlite3 stuct when you declare in viewController)

  3. Then Add Bridging-Header.h file (because sqlite is not written in swift)

  4. Bridging name should be your Projectname-Bridging-Header.h file (Just for naming convention, not mandatory)
  5. Write #import <sqlite3.h> in your Bridging-Header file
  6. Go to build settings tab

    (a) Under the build settings tab search for Swift Compiler - General option and set YES to Install Objective-C compatibility Header

    (b) Set your name and path for the header file in Objective-C Bridging Header option (Or you can simply drag the bridging header file)