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

后端 未结 4 1214
闹比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

    Mainly as a followup for future reference, @newacct's answer is the correct one. Since the original post showed a function that returns an array, the correct answer to this question is to tag the parameter with var:

    func lowPass(var vector:[CGFloat]) -> [CGFloat] {
        let blend:CGFloat = 0.2
    
        // Smoothens out the data input.
        vector[0] = vector[0] * blend + lastVector[0] * (1 - blend)
        vector[1] = vector[1] * blend + lastVector[1] * (1 - blend)
        vector[2] = vector[2] * blend + lastVector[2] * (1 - blend)
    
        // Sets the last vector to be the current one.
        lastVector = vector
    
        // Returns the lowpass vector.
        return vector
    }
    

提交回复
热议问题