示例 struct 关键字
struct Location {
let latitude: Double // 可以给默认值 = 0
let longitude: Double
}
let myHomeLocation = Location(latitude: 21, longitude: 33)
结构体之构造函数
第一种方式:
struct Location {
let latitude: Double = 0
let longitude: Double = 0
}
let myHomeLocation = Location() // 这里不需要传参 因为上面给了默认值
第二种方式:
struct Location {
var latitude: Double
var longitude: Double
var point: (Double, Double)?
init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
init(latitude: Double, longitude: Double, point: (Double, Double)) {
self.latitude = latitude
self.longitude = longitude
self.point = point
}
}
// 调用构造函数
let myLocation = Location(latitude: 20, longitude: 30, point: (20, 30))
let myHomeLation = Location(latitude: 20, longitude: 30)