I created a simple struct called ShoppingList.
struct ShoppingList {
var shoppingListId :NSNumber
var title :String
var groceryItems :[GroceryItem]
init() {
self.title = ""
self.groceryItems = [GroceryItem]()
self.shoppingListId = NSNumber(integer: 0)
}
}
Next I created a ShoppingList array like this:
var shoppingLists = [ShoppingList]()
After that I fetch the shopping lists etc. Now, I iterate through the shoppingLists and change the title but it ever updates the title property.
for var shoppingList in shoppingLists {
let items = getGroceryItemsByShoppingList(shoppingList)
shoppingList.groceryItems = getGroceryItemsByShoppingList(shoppingList)
shoppingList.title = "BLAH" // copied by value
print("ShoppingList \(shoppingList.title) has \(shoppingList.groceryItems.count) items") // THIS PRINT BLAH
}
print("shoppingLists[0].groceryItems.count \(shoppingLists[0].groceryItems.count)") // THIS PRINTS THE ORIGINAL CONTENT
I believe that when I am running the loop it is copying by value and hence the original array is never changed. How can I change the original array using For loop?