以下代码执行将输入coordinate
与几个预定义的coordinates
进行比较的功能。但是,当我尝试将coordinates
之一的字符串值转换为float
时,它会出现“错误:无法推断出通用参数'Element'”。
我想将数组的字符串元素转换为float
。
import UIKit
var coordinates: [[[String]]] = []
var nodeLetter:Array=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var nextValue = ""
var checkCoordinates = ["",""]
var distance = 100000
var xDifference = 0.0
coordinates = [[["A","0.000","0.000"]],[["B","55.5352431","58.4545"]],[["C","0.000","200.123"]]]
var inputCooridnate = ["65.123","62.0909"]
for amount in coordinates {
for elements in amount {
for items in elements{
if nextValue == "Y"{
checkCoordinates[1] = items
print("Y Value = \(items)")
nextValue = ""
print(("Pie= \(Double(inputCooridnate[0]))"))
xDifference =Double(inputCooridnate[0])
}
if nextValue == "X"{
checkCoordinates[0] = items
print("X Value = \(items)")
nextValue = "Y"
}
if nodeLetter.contains(items) {
print("Start Node \(items)")
nextValue = "X"
}
}
}
}
最佳答案
你能尝试改变吗
xDifference =Double(inputCooridnate[0])
与
guard let xDifference = Double(inputCooridnate[0]) else {
fatalError("Please check coordinates - cannot be nil")
}
为了防止出现nil值,使用
?
符号表示该值可能包含nil值。换句话说,它成为可选的。如果以后要使用它,则需要解开它(用!
),因此发誓是有价值的。另一种选择是使用guard let
-“可选绑定”的示例-一种更安全,更好的方法来防止值为nil的情况。请让我知道此解决方案是否有效。祝一切顺利!
关于ios - 错误:无法推断出通用参数“元素”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38245026/