modify an array within a function does not work

前端 未结 4 841
情深已故
情深已故 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:06

    You have misunderstood how arrays work in Swift. They are value types, which is unlike Objective-C where they are reference types.

    What this means is that the function alter_them gets a copy of the arrays and you are actually modifying the copy.

    You will need to return the modified versions from the alter_them function and re-assign Draw_S and Draw_E.

    FWIW instance variables usually start with a lowercase character i.e. draw_S

提交回复
热议问题