The reason you see this error is because petItem is immutable (you cannot change it) inside addItem . If you want to change the instance you are passing, you need to declare the function as follows:
func addItem(var petItem:Item) -> Bool { ...
Unfortunately, now you have one more error, since the variable petItem that you created initally with let is unchanged. So change this to var and release:
var petItem = Item(name:petName!.uppercaseString, type:petType, value:0, tag:0)
So now you can run your code, and you can understand that the name set to addItem() does not output it to the variable petItem . What happened? In Swift struct instances are passed to functions by value, so nothing that happens to a parameter in a function affects the original variable unless the parameter is declared as inout . So do this:
func addItem(inout petItem:Item) -> Bool { ...
and you will need to change your call to show that you know that this parameter can be changed:
if addItem(&petItem) { ...
Nate cook
source share