Swift function that takes in array giving error: '@lvalue $T24' is not identical to 'CGFloat'

后端 未结 4 1173
闹比i
闹比i 2021-01-23 08:58

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

4条回答
  •  盖世英雄少女心
    2021-01-23 09:40

    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:

    • Declare it 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.
    • Declare it 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

提交回复
热议问题