Optional

public extension Optional
public extension Optional where Wrapped: Collection
public extension Optional where Wrapped: RawRepresentable, Wrapped.RawValue: Equatable
  • SwifterSwift: Get self of default value (if self is nil).

    let foo: String? = nil
    print(foo.unwrapped(or: "bar")) -> "bar"
    
    let bar: String? = "bar"
    print(bar.unwrapped(or: "foo")) -> "bar"
    

    Declaration

    Swift

    func unwrapped(or defaultValue: Wrapped) -> Wrapped

    Parameters

    defaultValue

    default value to return if self is nil.

    Return Value

    self if not nil or default value if nil.

  • SwifterSwift: Gets the wrapped value of an optional. If the optional is nil, throw a custom error.

       let foo: String? = nil
       try print(foo.unwrapped(or: MyError.notFound)) -> error: MyError.notFound
    
       let bar: String? = "bar"
       try print(bar.unwrapped(or: MyError.notFound)) -> "bar"
    

    Throws

    The error passed in.

    Declaration

    Swift

    func unwrapped(or error: Error) throws -> Wrapped

    Parameters

    error

    The error to throw if the optional is nil.

    Return Value

    The value wrapped by the optional.

  • SwifterSwift: Runs a block to Wrapped if not nil

    let foo: String? = nil
    foo.run { unwrappedFoo in
        // block will never run sice foo is nill
        print(unwrappedFoo)
    }
    
    let bar: String? = "bar"
    bar.run { unwrappedBar in
        // block will run sice bar is not nill
        print(unwrappedBar) -> "bar"
    }
    

    Declaration

    Swift

    func run(_ block: (Wrapped) -> Void)

    Parameters

    block

    a block to run if self is not nil.

  • SwifterSwift: Assign an optional value to a variable only if the value is not nil.

    let someParameter: String? = nil
    let parameters = [String: Any]() // Some parameters to be attached to a GET request
    parameters[someKey] ??= someParameter // It won't be added to the parameters dict
    

    Declaration

    Swift

    static func ??= (lhs: inout Optional, rhs: Optional)

    Parameters

    lhs

    Any?

    rhs

    Any?

  • SwifterSwift: Assign an optional value to a variable only if the variable is nil.

    var someText: String? = nil
    let newText = "Foo"
    let defaultText = "Bar"
    someText ?= newText // someText is now "Foo" because it was nil before
    someText ?= defaultText // someText doesn't change its value because it's not nil
    

    Declaration

    Swift

    static func ?= (lhs: inout Optional, rhs: @autoclosure () -> Optional)

    Parameters

    lhs

    Any?

    rhs

    Any?

Available where Wrapped: Collection

  • SwifterSwift: Check if optional is nil or empty collection.

    Declaration

    Swift

    var isNilOrEmpty: Bool { get }
  • SwifterSwift: Returns the collection only if it is not nill and not empty.

    Declaration

    Swift

    var nonEmpty: Wrapped? { get }

Available where Wrapped: RawRepresentable, Wrapped.RawValue: Equatable

  • Returns a Boolean value indicating whether two values are equal.

    Equality is the inverse of inequality. For any values a and b, a == b implies that a != b is false.

    Declaration

    Swift

    @inlinable
    static func == (lhs: Optional, rhs: Wrapped.RawValue?) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns a Boolean value indicating whether two values are equal.

    Equality is the inverse of inequality. For any values a and b, a == b implies that a != b is false.

    Declaration

    Swift

    @inlinable
    static func == (lhs: Wrapped.RawValue?, rhs: Optional) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns a Boolean value indicating whether two values are not equal.

    Inequality is the inverse of equality. For any values a and b, a != b implies that a == b is false.

    Declaration

    Swift

    @inlinable
    static func != (lhs: Optional, rhs: Wrapped.RawValue?) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.

  • Returns a Boolean value indicating whether two values are not equal.

    Inequality is the inverse of equality. For any values a and b, a != b implies that a == b is false.

    Declaration

    Swift

    @inlinable
    static func != (lhs: Wrapped.RawValue?, rhs: Optional) -> Bool

    Parameters

    lhs

    A value to compare.

    rhs

    Another value to compare.