Using Arrays in WhereClauses

In Swift, is it possible to create a whereClause that uses an array? There is a post on this forum that discusses using arrays in whereClauses but I am having trouble with the syntax. Here is what I would like to do.

let whereClauseArray = ["Jon", "Mike", "Chris", "Lindsay", "Jeffrey"]


let whereClause = "username in whereClauseArray"
let dataQuery = BackendlessDataQuery()
dataQuery.whereClause = whereClause
            
self.backendless?.data.of(BackendlessUser.ofClass()).find(dataQuery, response: {(result : BackendlessCollection?) -> () in
                
                let currentUser = result?.data as! [BackendlessUser]
                
                for user in currentUser
                {
                    print(user)
                }
                
            }, error: {(fault : Fault?) -> () in
                
                print("There is an error: \(fault)")
            })

I know my trouble is here.

let whereClause = "username in whereClauseArray"

Is there a way to write this so that it compiles properly?

A where clause is just a string, it does not have any special provisions for Swift, Java or JS.

To check if value in a column is present in a collection of string literals, you can use the following syntax:

columnName in (‘Jon’, ‘Mike’, ‘Chris’)

Regards,
Mark

Thank you. What if you want to use a pre-populated array? The syntax you’ve used requires that you create the whereClause by typing the values. Is there a way to pass in an array instead of typing the values?

Write code that iterates over the array values to create string with the syntax I specified.

Thanks Mark.

This worked for me. I used the OR option versus (item0, item1, …) since the string creation was cleaner.

var usernameArray = ["Jon", "Lindsay", "Chris", "Mom", "Dad"]


var whileIndex = 0
var whereClause = String()


while whileIndex != usernameArray.endIndex
{
    if whileIndex != usernameArray.endIndex - 1
    {
        whereClause = whereClause + "username = '\(usernameArray[whileIndex])' OR "
        print(whereClause)
    }
    else if whileIndex == usernameArray.endIndex - 1
    {
        whereClause = whereClause + "username = '\(usernameArray[whileIndex])'"
        print(whereClause)
    }
   
    whileIndex += 1
}