我已经浏览了新的iOS8并阅读了有关扩展的内容。任何人都可以帮我创建扩展的样本吗?提前致谢
答案 0 :(得分:1)
此示例向Swift添加了五个计算实例属性 内置Double类型,为使用提供基本支持 距离单位:
extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
println("One inch is \(oneInch) meters")
// prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
println("Three feet is \(threeFeet) meters")
// prints "Three feet is 0.914399970739201 meters
摘自:Apple Inc.“The Swift Programming Language。”iBooks。 https://itun.es/ca/jEUH0.l