modify an array within a function does not work

前端 未结 4 845
情深已故
情深已故 2021-01-27 12:00

I call a function to process and modify an array. But the array does not change at all. Looks like a Swift major bug ???

var Draw_S = [String]();
var Draw_E =          


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-27 12:14

    This is not a bug. This is because arrays of strings in Swift are defined as structs and are therefore value types.

    The function is modifying the array that is passed in as a parameter but that is a copy of the array Draw_S and so on.

    You need to define them as inout parameters if you want the function to modify the existing array but that breaks the way Swift works. You'd be better passing a result array out and storing that instead.

提交回复
热议问题