swiftcocoapodssqlite.swift

How do I resolve "Cannot find connection in scope" error (SQLite)?


I'm trying to use sqlite.swift in a small app I'm developing, but I'm new to Swift and SQLite. I used CocoaPods to install sqlite.swift. I used these commands:

sudo gem install cocoapods
pod setup --verbose

I then navigated to the directory for my app and entered:

pod init
open -a Xcode Podfile

I then edited the pod as follows:

platform :ios, '9.0'

target 'GeneralPractice (iOS)' do
    pod 'SQLite.swift', '~> 0.13.1'
end

I then open the app workspace and add import sqlite3 to ContentView.swift, and then add this code before struct ContentView: View {:

let path = NSSearchPathForDirectoriesInDomains(
    .documentDirectory, .userDomainMask, true
).first!

let db = try Connection("\(path)/db.sqlite3")

When I run the app, I get this error:

"Cannot find 'Connection' in scope"

Given how new I am to this and to Swift, I suspect I'm making some simple error, but any help resolving this would be very much appreciated.


Solution

  • I finally solved this problem. As Kiril S. suggested, SQLite.swift only works if "import SQLite" is used. However, as far as I can tell, "import SQLite" can only be used without errors in a file that contains no Views. When I use "import SQLite" in a file with any View (like ContentView.swift) I get multiple error messages. If I create a new Swift file without any View, everything works fine.