Swift 中是否有双打占位符?

Is there a placeholder for doubles in Swift?

我希望能够调用集合中的各种替身,但我在设置时遇到了问题。

" " 可以用在 value/constant 声明的右侧作为字符串值的占位符,有没有办法对双精度数做同样的事情?我用谷歌搜索但找不到任何东西。

原题

“nan”“不是数字”具有未定义或无法表示的数字的特定含义。有静音和信号版本。你可以通过将 Double.infinity 乘以某个东西来得到一个 nan。

let alarmTriggeringNotANumber: Double = .signalingNan
let quietNotANumber: Double = .nan
let hero: Double = .zero
let loveLife: Double = -.infinity

从技术上讲,"" 不是占位符,而是一个空集合。 Nil 是表示值不存在的一种方式。

对于评论中的单独问题

I want to get a bunch of latitude and longitude coordinates and call them from a collection using one name. For example var latitude = x, where x would represent whichever object I call to it.

您可以创建一个 Dictionary/Set 元素类型为坐标的元素。

struct Coordinates {
 let lat: Double
 let long: Double
}

let batCavesByCountryID: [UUID : [Coordinates]]

您可以创建一个类型别名,以允许您引用具有特定签名的元组。

您还可以为 Lat/Long 创建类型别名。

类型别名有助于减少代码维护。例如,您可以将 ID 从 UUID 更改为字符串。使用 typealias 可以更有表现力,让你用一行代码改变整个应用程序。但是,过火只会使这一点变得模糊。您可以为类型别名添加扩展名,但元组目前不可扩展。

typealias Coordinates = (lat: Latitude, long: Longitude)
typealias Latitude = Double
typealias Longitude = Double