如何以正确的方式更新数组中的所有项目
How to update all items in Array in the correct way
有一组货币汇率,在 inputTextField 中输入金额后,我想更新此数组中的所有项目,并将其放入 table 视图中,试图做到这一点带循环,但这不能正常工作,因为那是解决方案,只是放在 table 每个循环
中
inputTextField 是文本字段的名称
receivedRates:[Double] = [1.1,1.6,2.0,1.3] // just example of the rates
for i in receivedRates {
let newValue = Double(i)*Double(Int(inputTextField.text!)!)
currentAmount.append(newValue)
receivedRates = currentAmount
}
如何在没有循环的情况下更新这个数组,或者这个循环是否有其他解决方案?
您想将数组中的所有项与文本字段的双精度值相乘。
Swift一个非常合适的方式是map
var receivedRates = [1.1, 1.6, 2.0, 1.3]
receivedRates = receivedRates.map{ [=10=] * Double(inputTextField.text!)! }
或者,您可以枚举索引并就地更新值
receivedRates.indices.forEach{ receivedRates[[=11=]] *= Double(inputTextField.text!)! }
我怀疑您是否真的想先将文本转换为 Int
(丢失小数部分),然后再转换为 Double
,当然您应该安全地解包选项。
你可以使用.map
函数
Loops over a collection and applies the same operation to each element
in the collection.
receivedRates = receivedRates.map{ [=10=] * Double(inputTextField.text!)!}
let newValue = Double(i)*Double(Int(inputTextField.text!)!)
此处的强制解包 (!
) 是一个警告标志,在您的应用程序运行时可能很容易损坏。
由于 inputTextField
实例的 text
属性 是可选的,您应该确保 安全地 在尝试解包之前解包它在 尝试 初始化 Double
实例中使用它。此外,尝试初始化的结果(也是可选的)也是如此。
您可以实现这种双重安全解包逻辑,例如使用可选绑定和 flatMap(...)
method of Optional
;此后您可以使用例如map
更新 receivedRates
数组的值:
var receivedRates = [1.1, 1.6, 2.0, 1.3]
if let rateFactor = inputTextField.text.flatMap(Double.init) {
receivedRates = receivedRates.map { [=11=] * rateFactor }
}
或者,元素突变到位(我看不出为什么不在这里使用 for .. in
循环):
if let rateFactor = inputTextField.text.flatMap(Double.init) {
for idx in receivedRates.indices {
receivedRates[idx] *= rateFactor
}
}
请注意,如果 text
属性 是 nil
或者 Double
实例的初始化失败,则数组将不会更新(可以在上面 if
语句的 else
子句中专门处理的情况。
可以使用map
代替for-in
循环,这样会快很多,但是你必须尽可能避免使用强制解包选项(!
),因为示例如下:
func updateRates(_ receivedRates: inout [Double], by text: String?) -> Bool {
guard text != nil, let inputValue = Double(text!) else { return false }
receivedRates = receivedRates.map{ [=10=] * inputValue }
return true
}
用法:
var receivedRates: [Double] = [1.1, 1.6, 2.0, 1.3]
if updateRates(&receivedRates, by: inputTextField.text) {
print(receivedRates)
}
另一个优点是您正在使用引用参数inout
更新原始receivedRates
数组
您还可以扩展 MutableCollection
并实现您自己的可变映射方法来转换您的数组:
extension MutableCollection {
mutating func mutableMap(_ transform: (Element) throws -> Element) rethrows {
var index = startIndex
for element in self {
self[index] = try transform(element)
formIndex(after: &index)
}
}
}
用法:
var receivedRates = [1.1, 1.6, 2.0, 1.3]
receivedRates.mutableMap { [=11=] * 2.5 }
print(receivedRates) // [2.75, 4, 5, 3.25]
你的情况:
if let value = Double(inputTextField.text!) {
receivedRates.mutableMap { [=12=] * value }
}
有一组货币汇率,在 inputTextField 中输入金额后,我想更新此数组中的所有项目,并将其放入 table 视图中,试图做到这一点带循环,但这不能正常工作,因为那是解决方案,只是放在 table 每个循环
中inputTextField 是文本字段的名称
receivedRates:[Double] = [1.1,1.6,2.0,1.3] // just example of the rates
for i in receivedRates {
let newValue = Double(i)*Double(Int(inputTextField.text!)!)
currentAmount.append(newValue)
receivedRates = currentAmount
}
如何在没有循环的情况下更新这个数组,或者这个循环是否有其他解决方案?
您想将数组中的所有项与文本字段的双精度值相乘。
Swift一个非常合适的方式是map
var receivedRates = [1.1, 1.6, 2.0, 1.3]
receivedRates = receivedRates.map{ [=10=] * Double(inputTextField.text!)! }
或者,您可以枚举索引并就地更新值
receivedRates.indices.forEach{ receivedRates[[=11=]] *= Double(inputTextField.text!)! }
我怀疑您是否真的想先将文本转换为 Int
(丢失小数部分),然后再转换为 Double
,当然您应该安全地解包选项。
你可以使用.map
函数
Loops over a collection and applies the same operation to each element in the collection.
receivedRates = receivedRates.map{ [=10=] * Double(inputTextField.text!)!}
let newValue = Double(i)*Double(Int(inputTextField.text!)!)
此处的强制解包 (!
) 是一个警告标志,在您的应用程序运行时可能很容易损坏。
由于 inputTextField
实例的 text
属性 是可选的,您应该确保 安全地 在尝试解包之前解包它在 尝试 初始化 Double
实例中使用它。此外,尝试初始化的结果(也是可选的)也是如此。
您可以实现这种双重安全解包逻辑,例如使用可选绑定和 flatMap(...)
method of Optional
;此后您可以使用例如map
更新 receivedRates
数组的值:
var receivedRates = [1.1, 1.6, 2.0, 1.3]
if let rateFactor = inputTextField.text.flatMap(Double.init) {
receivedRates = receivedRates.map { [=11=] * rateFactor }
}
或者,元素突变到位(我看不出为什么不在这里使用 for .. in
循环):
if let rateFactor = inputTextField.text.flatMap(Double.init) {
for idx in receivedRates.indices {
receivedRates[idx] *= rateFactor
}
}
请注意,如果 text
属性 是 nil
或者 Double
实例的初始化失败,则数组将不会更新(可以在上面 if
语句的 else
子句中专门处理的情况。
可以使用map
代替for-in
循环,这样会快很多,但是你必须尽可能避免使用强制解包选项(!
),因为示例如下:
func updateRates(_ receivedRates: inout [Double], by text: String?) -> Bool {
guard text != nil, let inputValue = Double(text!) else { return false }
receivedRates = receivedRates.map{ [=10=] * inputValue }
return true
}
用法:
var receivedRates: [Double] = [1.1, 1.6, 2.0, 1.3]
if updateRates(&receivedRates, by: inputTextField.text) {
print(receivedRates)
}
另一个优点是您正在使用引用参数inout
更新原始receivedRates
数组
您还可以扩展 MutableCollection
并实现您自己的可变映射方法来转换您的数组:
extension MutableCollection {
mutating func mutableMap(_ transform: (Element) throws -> Element) rethrows {
var index = startIndex
for element in self {
self[index] = try transform(element)
formIndex(after: &index)
}
}
}
用法:
var receivedRates = [1.1, 1.6, 2.0, 1.3]
receivedRates.mutableMap { [=11=] * 2.5 }
print(receivedRates) // [2.75, 4, 5, 3.25]
你的情况:
if let value = Double(inputTextField.text!) {
receivedRates.mutableMap { [=12=] * value }
}