Mutable Array in Swift

How can I use a Mutable Strongly Type array in Swift? I am adding ShoppingList object to an Array like this:

 func getAllShoppingLists() -> Array<ShoppingList> {

        let shoppingLists = Array<ShoppingList>()

        db.open()

        let results = try! db.executeQuery("SELECT * from ShoppingLists", values: nil)

        defer {

            db.close()
        }

        while(results.next()) {

            let shoppingList = ShoppingList()
            shoppingList.title = results.stringForColumn("title")

            shoppingLists.append(shoppingList)
        }

        return shoppingLists
    }

The problem comes that the shoppingLists is declared as "let". If I change to "var" it works fine. The reason of course is that I am changing the number of items in the array. Is this the best way?


You can create this code,

var shoppingArray = [ShoppingList]()
shoppingArray.append(yourInstance)

That is the only way to make an array mutable, to understand the difference between let and var, see this post, as the answer explains it in detail.

What is the difference between `let` and `var` in swift?