无法理解为什么 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
的内置方法。 String
到 NSString
到 Double
是标准的解决方法。
我的版本是这样的:
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/