斯威夫特函数,它在阵列给错误:“@lvalue $ T24'不等同于'CGFloat的” [英] Swift function that takes in array giving error: '@lvalue $T24' is not identical to 'CGFloat'

查看:196
本文介绍了斯威夫特函数,它在阵列给错误:“@lvalue $ T24'不等同于'CGFloat的”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在写一个低通加速功能中度加速度计的抖动。我有一个CGFloat的阵列重新present数据,我想用这个功能可以阻尼吧:

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 function:

// Damps the gittery motion with a lowpass filter.
func lowPass(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
}

在这种情况下,lastVector定义为在我的节目的顶部跟进:

In this case, lastVector is defined as follows up at the top of my program:

var lastVector:[CGFloat] = [0.0, 0.0, 0.0]

三线的形式载体[A] = ...给我的错误。任何想法,为什么我收到这个错误?

The three lines in the form vector[a] = ... give me the errors. Any ideas as to why i am getting this error?

推荐答案

这code似乎如果你通过与 INOUT 修改数组进行编译:

That code seems to compile if you pass the array with the inout modifier:

func lowPass(inout vector:[CGFloat]) -> [CGFloat] {
    ...
}

我不知道这是否是一个错误或没有。出于本能,如果我通过一个数组功能我希望能够对其进行修改。如果我通过与 INOUT 修改,我希望能够使原来的变量指向一个新的阵列 - 类似于在 &安培; 修改器中的C和C ++

I'm not sure whether that's a bug or not. Instinctively, if I pass an array to a function I expect to be able to modify it. If I pass with the inout modifier, I'd expect to be able to make the original variable to point to a new array - similar to what the & modifier does in C and C++.

也许背后的原因是,在斯威夫特有可变和不可变阵列(和字典)。如果没有 INOUT 它被认为不可变的,因此为什么它不能被修改的原因。

Maybe the reason behind is that in Swift there are mutable and immutable arrays (and dictionaries). Without the inout it's considered immutable, hence the reason why it cannot be modified.

附录1 - 这是不是一个错误

@newacct说这是预期的行为。经过一番研究,我同意他的观点。但是,即使不是一个错误我原本认为这是错误的(读取多达月底结论的)。

@newacct says that's the intended behavior. After some research I agree with him. But even if not a bug I originally considered it wrong (read up to the end for conclusions).

如果我有这样一个类:

class WithProp {
    var x : Int = 1

    func SetX(newVal : Int) {
        self.x = newVal
    }
}

我可以传递那个类的一个实例的功能,并且该功能可以修改它的内部状态

I can pass an instance of that class to a function, and the function can modify its internal state

var a = WithProp()

func Do1(p : WithProp) {
    p.x = 5 // This works
    p.SetX(10) // This works too
}

而无需通过实例为 INOUT
我可以使用 INOUT 而不是让 A 变量指向另一个实例:

without having to pass the instance as inout. I can use inout instead to make the a variable to point to another instance:

func Do2(inout p : WithProp) {
    p = WithProp()
}

Do2(&a)

使用了code,从 DO2 中我做了 P 参数(即 A 变量)指向一个新创建的实例 WithProp

With that code, from within Do2 I make the p parameter (i.e. the a variable) point to a newly created instance of WithProp.

同样不能用一个数组来完成(我presume字典以及)。要改变其内部状态(修改,增加或删除元素)必须使用 INOUT 修改。这是违反直觉的。

The same cannot be done with an array (and I presume a dictionary as well). To change its internal state (modify, add or remove an element) the inout modifier must be used. That was counterintuitive.

但是,一切都被看完后<澄清href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-XID_110\">this从迅速书摘

But everything gets clarified after reading this excerpt from the swift book:

斯威夫特的字符串,数组和字典类型实现的结构。这意味着,当它们被分配到一个新的常数或变量,或当它们被传递给函数或方法

Swift’s String, Array, and Dictionary types are implemented as structures. This means that strings, arrays, and dictionaries are copied when they are assigned to a new constant or variable, or when they are passed to a function or method.

所以传递到FUNC时,它不是原始数组,但它的一个副本 - 因此,任何改变(即使可能)不会被原来的阵列上完成制作

So when passed to a func, it's not the original array, but a copy of it - Hence any change made to it (even if possible) wouldn't be done on the original array.

所以,最后,我原来的答复上面的正确并有经验的行为的不是一个错误

So, in the end, my original answer above is correct and the experienced behavior is not a bug

非常感谢@newacct:)

Many thanks to @newacct :)

这篇关于斯威夫特函数,它在阵列给错误:“@lvalue $ T24'不等同于'CGFloat的”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆