So I\'m writing a lowpass accelerometer function to moderate the jitters of the accelerometer. I have a CGFloat array to represent the data and i want to damp it with this funct
Since Xcode 6 beta 3, modifying the contents of an Array
is a mutating operation. You cannot modify a constant (i.e. let
) Array; you can only modify a non-constant (i.e. var
) Array.
Parameters to a function are constants by default. Therefore, you cannot modify the contents of vector
since it is a constant. Like other parameters, there are two ways to be able to change a parameter:
var
, in which case you can assign to it, but it is still passed by value, so any changes to the parameter has no effect on the calling scope.inout
, in which case the parameter is passed by reference, and any changes to the parameter is just like you made the changes on the variable in the calling scope.You can see in the Swift standard library that all the functions that take an Array and mutate it, like sort()
, take the Array as inout
.
P.S. this is just like how arrays work in PHP by the way