我嘗試寫這樣的代碼在SWIFT 2:自斯威夫特2
//: Playground - noun: a place where people can play
import Foundation
struct Point {
var x: Double
var y: Double
}
func distanceTo (#point: Point) -> Double {
let a = abs(self.x - point.x)
let b = abs(self.y - point.y)
let c = sqrt(a * a + b * b)
return c
}
let pointA = Point(x: 1.0, y: 2.0)
let pointB = Point(x: 4.0, y: 6.0)
let distance = pointA.distanceTo (point: pointB)
,但我得到這個錯誤:
#has been remove from swift, so i change the code to
func distanceTo (#point: Point) -> Double
但後來我得到了另一個錯誤:
use of unresolved identifier 'self'
任何線索如何解決這個錯誤在swift 2?
感謝名單
僅供參考,而不是'開方(a * a + b * b)'你也可以使用'hypot(a,b)'。 – Rob