swift - Xcode6 无法使用类型的参数列表调用 'subscript'

标签 swift invoke

无法理解为什么 xcode 会尝试调用下标,我不希望它来自 xcode。

我有一个简单的结构:

struct Point3D
{
    var x: Double = 0.0
    var y: Double = 0.0
    var z: Double = 0.0
    init(x:Double, y:Double, z:Double) {self.x = x; self.y = y; self.z = z}
}

但是下面的代码不起作用,它说:无法使用类型为“(x: Double, y: Double, z: Double)”的参数列表调用“下标”。但如您所见,我有一个包含这些类型的 init...

private func convertFileStringToPoint3D(str:String)->Point3D
{
    let components_file_string_point3d = str.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: " \t"))
    if components_file_string_point3d.count>2 {
        return Point3D(x: Double(components_file_string_point3d[0]), y: Double(components_file_string_point3d[1]), z: Double(components_file_string_point3d[2]))
    } else {
        assertionFailure("Wrong File Structure. Cannot convert string to Point3D.")
    }
}

当我尝试使用 NSString 的 doublevalue 时,它​​说它没有名为 doublevalue 的成员...

我很尴尬 :( 我只是错过了一个字符 doublevalue 而不是 doubleValue...这是重复的,所以请删除,没有问题,只是错误...

最佳答案

与 Matt Gibson 所说的一样,Double(value: String) 不存在。到目前为止,还没有将 String 转换为 Double 的内置方法。 StringNSStringDouble 是标准的解决方法。

我的版本是这样的:

private func convertFileStringToPoint3D(str:String)->Point3D
{
    let components_file_string_point3d = str.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: " \t"))
    if components_file_string_point3d.count>2 {
        return Point3D(
            x: Double((components_file_string_point3d[0] as NSString).doubleValue),
            y: Double((components_file_string_point3d[1] as NSString).doubleValue),
            z: Double((components_file_string_point3d[2] as NSString).doubleValue)
        )
    } else {
        assertionFailure("Wrong File Structure. Cannot convert string to Point3D.")
    }
}

我想这就是您希望代码如何工作。

关于swift - Xcode6 无法使用类型的参数列表调用 'subscript',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26414055/

相关文章:

ios - Firebase AppCheck 的目的是什么?

uitableview - 不想在播放流媒体后等待 3 秒调用 didSelectRowAtIndexPath

ios - OpenWeatherMap 中不同大小的天气图标

c# - 根据 () => func 创建函数

c# - MethodInvoker 与 Control.BeginInvoke 的操作

ios - 我们如何使用 swift 和 iOS 11+ 进行直线图像转换

swift - 如何在 Swift 中处理与结构体的关系?

error-handling - AWS Lambda : access the invocation parameters from error handler

javascript - 从另一个未被调用的 Lambda 函数中调用 Lambda 函数

c# - 如何避免 UI 因调用而挂起?