2014-10-16 4 views
0

xcode가 xcode에서 아래 첨자를 호출하려고 시도하는 이유를 이해할 수 없습니다. xcode에서 원하지 않습니다.Xcode6 'subscript'를 호출 할 수 없습니다.

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

코드에서 작동하지 않습니다 그러나, 그것은 말한다 : Cannot invoke 'subscript' with an argument list of type '(x: Double, y: Double, z: 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]), y: Double(components_file_string_point3d[1]), z: Double(components_file_string_point3d[2])) 
    } else { 
     assertionFailure("Wrong File Structure. Cannot convert string to Point3D.") 
    } 
} 

을 이러한 유형과 초기화를 그리고 나는 그것이있는 doubleValue라는 멤버를 가지고 있지 않았다고 말한다는 NSString의있는 doubleValue를 사용하려고하면 ...

나는 두렵다. (나는 방금 doubleValue 대신에 한 글자 doublevalue를 놓쳤다 ... 이것은 중복 되었으니 삭제 해 주시길 바랍니다. 아무런 질문이 없으니 실수는 아님. ...

+1

문제는 Point3D 클래스의 init 때문이 아닙니다. Double에는 String을 사용하는 init이 없기 때문입니다. –

+0

http://stackoverflow.com/questions/24031621/swift-how-to-convert-string-to-double ... –

+0

typechecker에게 당신이 바로 여기 있다고 확신 시키십시오. let components_file_string_point3d : [NSString]' , 그리고 나서'doubleValue'를 호출하십시오 – CodaFi

답변

0

스위프트의 더블에는 이니셜 라이저가 없습니다 NSString의 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) 

components_file_string_point3d의 형식을 명시 적으로 지정 했으므로 Swift String이 아닌 NSString의 배열입니다. 나중에 쉽게 NSString의 doubleValue 메서드에 액세스 할 수 있습니다.

+0

'[NSString]'그것은 여분의, 그것은 완벽하게 작동하지 않고 오류가 발생합니다 –

+0

@ OlexiyPyvovarov? 위에서 작성한대로 놀이터에서 코드를 테스트했는데 정상적으로 작동합니다. 명시 적 [NSString] 입력을 제거하면 "error : 'String'에 doubleValue를 사용할 때 'doubleValue'라는 멤버가 없습니다. 어떤 Xcode 버전을 사용하고 있습니까? 컴파일러 (6.1)의 베타 버전이나 릴리스 버전 (6.0.1)을 사용하고 있습니까? 나는 6.0.1을 사용하고있다. –

2

매트 깁슨 (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.") 
    } 
} 

그것은 모든 당신이 당신의 코드가 같아요 작동하는 방법입니다.

+0

또한 작동하지만 너무 많은 다운 캐스팅이 필요하지 않음) –