modify an array within a function does not work

前端 未结 4 846
情深已故
情深已故 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条回答
  •  猫巷女王i
    2021-01-27 12:06

    The arrays inside alter_them are a copy of the originals.

    Use inout to modify the original arrays:

    func alter_them(inout data: [String], inout data2: [String]){
        for (i, _) in data.enumerate(){
            data[i] = "1"
        }
        for (i, _) in data2.enumerate(){
            data2[i] = "2"
        }
    }
    
    alter_them(&Draw_S, data2: &Draw_E)
    

提交回复
热议问题