Functions
The following functions are available globally.
-
Returns a measurement raised to the given power.
Raising a measurement affects only its value and keeps the original unit.
let measurement = pow(Measurement<UnitInformationStorage>(value: 2, unit: .bits), 3) let limit = Measurement<UnitInformationStorage>(value: 1, unit: .bytes) if limit <= measurement { print("Large enough data: \(measurement)") // Prints: Large enough data: 8.0 bit }
-
Returns the square root of a measurement.
Square root of a measurement affects only its value and keeps the original unit.
let measurement = sqrt(Measurement<UnitInformationStorage>(value: 64, unit: .bits)) let limit = Measurement<UnitInformationStorage>(value: 1, unit: .bytes) if measurement <= limit { print("Small enough data: \(measurement)") // Prints: Small enough data: 8.0 bit }
-
Performs a
nil
checking operation, returning the wrapped value of anOptional
instance or throwing the given error.The
?!
operator unwraps the left-hand side if it has a value, or it throws the right-hand side. The result of this operation will have the non-optional type of the left-hand side’sWrapped
type.Like the
??
operator, this operator uses short-circuit evaluation. For example:struct GenericError: Error {} var called = false func error() -> Error { called = true return GenericError() } let goodNumber = try Int("100") ?! error() // goodNumber == 100 // called == false do { let _ = try Int("invalid-input") ?! error() // Throws GenericError } catch { // called == true }
Declaration
Swift
public func ?! <Value, Error>(optional: Optional<Value>, error: @autoclosure () -> Error) throws -> Value where Error : Error
Parameters
optional
An optional value.
error
The error to throw if the optional value is
nil
.