gpt4 book ai didi

swift - 使用 Swift 的扩展示例

转载 作者:行者123 更新时间:2023-11-30 10:23:27 24 4
gpt4 key购买 nike

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

最佳答案

This example adds five computed instance properties to Swift’s built-in Double type, to provide basic support for working with distance units:

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.“Swift 编程语言”。 iBooks。 https://itun.es/ca/jEUH0.l

关于swift - 使用 Swift 的扩展示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24115554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com