使用Swift扩展示例

时间:2014-06-09 07:24:40

标签: swift ios8 ios-app-extension

我已经浏览了新的iOS8并阅读了有关扩展的内容。任何人都可以帮我创建扩展的样本吗?提前致谢

1 个答案:

答案 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