Swift中带有数组的For-In循环中Iterator元素的可变性 [英] Mutability of the Iterator Element in a For-In loop with an Array in Swift

查看:205
本文介绍了Swift中带有数组的For-In循环中Iterator元素的可变性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift 3.0中有一些代码,例如尝试更新元素数组中的属性...

I have some code in Swift 3.0 like so for trying to update the property in a array of elements...

for point in listOfPoints {
    var pointInFrame : Float = Float(point.position.x * sensorIncomingViewPortSize.width) + Float(point.position.y)
    point.status = getUpdateStatus( pointInFrame )
}

但是我得到以下编译错误: 无法分配给属性:'point'是'let'常量" [对于第3行]

However I get a compile error of: 'Cannot assign to property: 'point' is a 'let' constant' [for line 3]

无论如何,有没有在Swift中使迭代器(点)可变的方法,例如如何将'inout'用作函数参数?

Is there anyway to make the iterator (point) mutable in Swift, like how you can use 'inout' for a function parameter?

还是您应该以其他方式执行此任务?

Or are you supposed to do this task another way?

预先感谢.

斯坦

推荐答案

只需将其更改为var,而不是您在其中声明pointlet. letconstant.

Just change it to var instead of let where you declare point. let is a constant.

这篇关于Swift中带有数组的For-In循环中Iterator元素的可变性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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