I'm having difficulty with this function:
func sort(source: Array<Int>!) -> Array<Int>! {
source[0] = 1
......
return source
}
An error happens:
Why can't I directly assign the value to the specific element in the array?
The variable sort is immutable because it's a parameter. You need to create a mutable instance. Also, there's no reason to have the parameter and return value as implicitly unwrapped optionals with the ! operator.
func sort(source: Array<Int>) -> Array<Int> {
var anotherSource = source // mutable version
anotherSource[0] = 1
......
return anotherSource
}
Array<Int> is [Int]
inoutkeyword:func sort(inout source: [Int])and call it like thissort(source: &myArray).sourceisnilor empty.