Bubble sorting an array in Swift, compiler error on swap

前端 未结 6 1300
闹比i
闹比i 2021-01-20 05:22

I wrote a really simple bubble sort for a card game. It takes an array of \"Card\" objects, each of which has a an \"order\" attribute which indicates the value to be sorted

6条回答
  •  渐次进展
    2021-01-20 05:41

    Function parameters are by default constant (as if declared with let). If you want to modify the parameter inside your function, you have to declare it as a variable:

    func sortCards(var cards: Array) -> Array { ...
    

    Note that only the local parameter cards is modified, not the array passed as an argument to the function (which seems to be your intention because the function returns a new array).

提交回复
热议问题