Can't assign an string to an array element inside my Swift function -


Both assignments in the code below give me this error:

'@ | Value $ T5 is not the same as 'string'

  func printshopping list (myList: Array & lt; string & gt;) {println (myList) var cabbage = "cabbage" fish: string = "Fish" my list [0] = cabbage my list [1] = fish}  

What's wrong?

Parameters are static by default and you can not change them. You can change them to var , but this will be a copy of the array.

  func PrintShoppingList (var myList: Array & lt; string & gt;) {println (myList) var cabbage = "cabbage" fish: string = "fish" my list [0] = Cabbage My List [1] = Fish}  

You can also use the inout parameter to change the original value, but remember that the array length Superscript can not be extended, so myList [1] = fish will crash in this case.

  func PrintShoppingList (inout my list: Array end ) {println (myList) var cabbage = "cabbage" fish: string = "fish" my list [0] = Cabbage My List.Append (Fish)}  

Comments