Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Can't Import Sqlite3 iOS

Tags:

ios

swift

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'

like image 651
PREMKUMAR Avatar asked Jun 05 '14 16:06

PREMKUMAR


3 Answers

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
like image 71
David Berry Avatar answered Oct 29 '22 22:10

David Berry


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

like image 7
Ben Avatar answered Oct 30 '22 00:10

Ben


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!

like image 5
Rico Nguyen Avatar answered Oct 29 '22 22:10

Rico Nguyen