0

Im using the framework SQLite from stephencelis

I have the problem that the commands can be only used within viewDidLoad(). But i have to use the defined variables (e.g. var carlist) in functions outside viewDidLoad.

Where is my logical problem?

My code snipped:

    override func viewDidLoad() {
     super.viewDidLoad()
     let db = Database("/Users/**/Desktop/NEW/car/car.sqlite")      
     let cars = db["cars"]
     let car_id = Expression<Int64>("car_id")
     let car_model = Expression<String?>("car_model")

     for car_list in cars {
        println("car_model: \(car_list[car_model])")
        var carlist[car_list[car_model]] // array
     }
    }

The error, if i move the lines after "let db" to outside: 'ViewController.Type' does not have a member named "db"

0

2 Answers 2

1

Your problem is, that you declare the variable inside the viewDidLoad method and Swift doesn't see the variable. You have to declare it outside of a method like that:

class YourClass{
 //The DB-declaration
 let db = Database("/Users/**/Desktop/NEW/car/car.sqlite")

    override func viewDidLoad() {
     super.viewDidLoad()

     let cars = db["cars"]
     let car_id = Expression<Int64>("car_id")
     let car_model = Expression<String?>("car_model")

     for car_list in cars {
        println("car_model: \(car_list[car_model])")
        var carlist[car_list[car_model]] // array
     }
    }

}

As you see, I've declared the variable outside of any function. That way you can access from anywhere inside your class. There are different kind of variables. You can see it like that: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, my problem is now solved. Was my fault, logical problem
1

Christian's answer is halfway there. The actual problem is the line that assigns cars. The db declaration cannot be referenced in the class scope directly because it's an instance variable. If you want cars to be a helper throughout the entire class, you can most simply use a lazy variable:

class MyViewController: UIViewController {

    let db = Database("/Users/**/Desktop/NEW/car/car.sqlite")
    lazy var cars = self.db["cars"]
    let car_id = Expression<Int64>("car_id")
    let car_model = Expression<String?>("car_model")

    override func viewDidLoad() {
        super.viewDidLoad()

        for car_list in cars {
            println("car_model: \(car_list[car_model])")
        }
    }

}

Alternatively, you could make it a computed variable (but this is less efficient because it'll initialize a new value each time):

var cars: Query { return db["cars"] }

Or you can assign the variable on viewDidLoad:

var cars: Query!

override func viewDidLoad() {
    super.viewDidLoad()

    cars = db["cars"]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.