Search code examples
swiftsqliteopaque-pointers

Struggling with Opaque Pointers in SQLITE swift


Hi I am new to swift and attempting to add a database for my a level coding project. I cannot seem to get opaque pointers to work without errors. I have used tutorials and always get the same errors. The errors that I get:

Expected member name or constructor call after type name on line 3
Cannot convert value of type 'UnsafeMutablePointer<OpaquePointer?.Type>' to expected argument type 'UnsafeMutablePointer<OpaquePointer?>' On line 9
Cannot convert return expression of type 'OpaquePointer?.Type' to return type 'OpaquePointer?' On line 12
    var dbURL = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    
    func createDatabase() -> OpaquePointer?{
        var db = OpaquePointer?
        let url = NSURL(fileURLWithPath: dbURL)
        
        if let pathComponent = url.appendingPathComponent("Database.sqlite"){
            let filePath = pathComponent.path
            if sqlite3_open(filePath, &db) == SQLITE_OK{
                print("successfully opened database")
                
                return db
            }
          
        }
        
    }

Solution

  • Change var db = OpaquePointer? to var db : OpaquePointer?

    Add return nil to the end of the function.