0

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 = [String]();
override func viewDidLoad() {
    super.viewDidLoad()
    Draw_E.append("E")
    Draw_E.append("E")
    Draw_E.append("E")
    Draw_E.append("E")
    Draw_E.append("E")

    Draw_S.append("S")
    Draw_S.append("S")
    Draw_S.append("S")
    Draw_S.append("S")
    Draw_S.append("S")
    alter_them(Draw_S, data2: Draw_E)
    for (ix, _) in Draw_S.enumerate(){
        print("index: \(ix) array S: \(Draw_S[ix]) array E: \(Draw_E[ix])")
    }
}
func alter_them( var data: [String], var data2: [String]){
    for (i, _) in data.enumerate(){
        data[i] = "1"
    }
    for (i, _) in data2.enumerate(){
        data2[i] = "2"
    }
}

The result after calling the function shows original array content.

1
  • 2
    Apply Occam's Razor before saying that you found a major bug that looks like it would break almost every app written if it were an actual bug. Commented Sep 23, 2015 at 11:13

4 Answers 4

9

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)
Sign up to request clarification or add additional context in comments.

4 Comments

This would work but to me it feels a bit hacky. Would be better to create a "modify" function that takes and array and returns the modified array I think.
I agree with @Fogmeister. The approach in this answer, while "correct" and making it more "Objective-C like" it goes against the functional programming nature of Swift.
@EricD. fair enough :) I upvoted because it would work. Just not an ideal approach. But that's for the OP to decide :)
this worked. I guess I was confused by Xcode suggesting I use "var" in function declaration. Problem arose from converting some working java/android code where what I was doing was perfectly legal
3

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

Comments

0

Both String and Array are value types in Swift. That is the reason why it is not modified inside a method. You would need to return the modified array back from the method.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.