ILoveChickenWings
ILoveChickenWings

Reputation: 57

Is there a placeholder for doubles in Swift?

I want to be able to call various doubles from a collection but I'm having trouble setting it up.

" " can be used on the right side of a value/constant declaration as a placeholder for string values, is there a way to do the same for doubles? I Googled but wasn't able to find anything.

Upvotes: 0

Views: 106

Answers (1)

Ryan
Ryan

Reputation: 1392

For original question

A "nan" "not a number" has a specific meaning of an undefined or unrepresentable number. There are quiet and signaling versions. You can get a nan by multiplying Double.infinity by something.

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

Technically, "" isn't a placeholder, but an empty collection. Nil would be a way to indicate the absence of a value.

For the separate question in the comment

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.

You can create a Dictionary/Set where the element is of type Coordinates.

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

let batCavesByCountryID: [UUID : [Coordinates]]

You can create a type alias that allows you to reference a tuple with a specific signature.

You can also create a typealias for Lat/Long.

Typealiases are useful to reduce code maintenance. For example, you might change IDs from UUIDs to Strings. Using a typealias can be more expressive and let you change a whole app with one line of code. However, going overboard will just make this obscure. You can add extensions to type aliases, but tuples are currently not extendable.

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

Upvotes: 2

Related Questions