1

I need to use for loop get values from database to show in multiple variables What should i do? help me please.

My Variables :

var printerName1: String = ""
var printerName2: String = ""
var printerName3: String = ""
var printerName4: String = ""
var printerName5: String = ""

My Function :

func load(){

    var selectPrinter = [printerName1,printerName2,printerName3
     ,printerName4,printerName5]

    let db = SQLiteDB.shared
    db.openDB()
    let data = db.query(sql: "select * from tbPrinter")
    for index in 0..<data.count {
        let row = data[index]
        if let printerName = row["name"]{
            selectPrinter[index] = printerName as? String //Cannot to set value in variables
        }
    }
}

1 Answer 1

1

If you want to set the text value of the textfield by index you should add them to an array and then you can use the same index of the resultset to set the text.

let printers = [
    printerName1, 
    printerName2, 
    printerName3, 
    printerName4, 
    printerName5
]

func load() {
    let db = SQLiteDB.shared
    db.openDB()
    let data = db.query(sql: "SELECT * FROM tbPrinter")
    for index in 0..<data.count {
        let row = data[index]
        if let printerName = row["name"] {
            printers[index].text = printerName as? String
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

How to if I switched to use the variables instead of outlets

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.