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

后端 未结 4 1177
闹比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:41

    Edit: The following worked for Xcode Beta 2. Apparently, the syntax and behavior of arrays has changed in Beta 3. You can no longer modify the contents of an array with subscripts if it is immutable (a parameter not declared inout or var):

    Not valid with the most recent changes to the language

    The only way I could get it to work in the play ground was change how you are declaring the arrays. I suggest trying this (works in playground):

    import Cocoa
    let lastVector: CGFloat[] = [0.0,0.0,0.0]
    
    func lowPass(vector:CGFloat[]) -> CGFloat[] {
        let blend: CGFloat = 0.2
        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)
        return vector
    }
    
    var test = lowPass([1.0,2.0,3.0]);
    

提交回复
热议问题