ios - 错误:无法推断出通用参数“元素”

标签 ios swift casting nsarray

以下代码执行将输入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/

相关文章:

c++ - 将 unsigned char 转换为整数类型的背景中发生了什么?

casting - 如何使用 rust-xcb 获取给定窗口 ID 的 X 窗口类?

ios - 来自 App Delegate 的启动任务并避免竞争条件的策略

ios - 在没有高度限制的情况下使用自动布局为 View 的高度设置动画

swift - 如何使用 KVO observe 协议(protocol)的属性?

ios - 由字符串分隔的组件崩溃应用程序 swift

iphone - nsuserdefaults 的问题

javascript - 如何在 phonegap 的 ios7 之上添加永久线路

swift - 如何显示枚举的值

ios - 在 objective-c 中进行类型转换,(NSInteger) VS。整数值