0

Cannot understand why xcode tries to invoke a subscript, I do not want it from xcode.

I have a simple structure:

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}
}

However the code under doesn't working, it says: Cannot invoke 'subscript' with an argument list of type '(x: Double, y: Double, z: Double)'. But as you can see, I have an init with these types...

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.")
    }
}

And when I try to use doublevalue of NSString it says that it didn't have a member called doublevalue...

I'm so embaressed :( I just missed with one character doublevalue instead doubleValue... This is duplicate, so delete please, there is no question, ust mistake...

Olexiy Pyvovarov
  • 750
  • 2
  • 16
  • 29

2 Answers2

2

In the same vain as what Matt Gibson said, Double(value: String) does not exist. As of now there is no built in way to convert from String to Double. String to NSString to Double is the standard workaround.

My version of that looks like this:

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.")
    }
}

It's all how you want your code to work I guess.

Jeffery Thomas
  • 40,388
  • 8
  • 88
  • 114
0

Swift's Double doesn't have an initialiser from String. I'd use NSString's doubleValue:

    let components_file_string_point3d:[NSString] = str.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: " \t"))
    if components_file_string_point3d.count>2 {
        return Point3D(x: components_file_string_point3d[0].doubleValue, y: components_file_string_point3d[1].doubleValue, z: components_file_string_point3d[2].doubleValue)

Note that I'm explicitly specifying the type of components_file_string_point3d so that it's an array of NSString, not Swift String; that lets you access NSString's doubleValue method easily later.

Matt Gibson
  • 36,429
  • 9
  • 89
  • 124
  • `[NSString]` it's extra, it causes error, without it working perfectly – Olexiy Pyvovarov Oct 16 '14 at 21:45
  • @OlexiyPyvovarov ? I tested my code in a playground as written above and it works fine. If I remove the explicit [NSString] typing, I get "error: 'String' does not have a member named 'doubleValue'" when I use doubleValue. What version of Xcode are you using? Are you using the beta version of the compiler (6.1) or the release version (6.0.1)? I'm using 6.0.1. – Matt Gibson Oct 17 '14 at 06:46