Int

extension Int
  • Determine if self is even (equivalent to self % 2 == 0)

    Declaration

    Swift

    public var isEven: Bool { get }
  • Determine if self is odd (equivalent to self % 2 != 0)

    Declaration

    Swift

    public var isOdd: Bool { get }
  • Determine if self is positive (equivalent to self > 0)

    Declaration

    Swift

    public var isPositive: Bool { get }
  • Determine if self is negative (equivalent to self < 0)

    Declaration

    Swift

    public var isNegative: Bool { get }
  • Convert self to a Double.

    Most useful when operating on Int optionals.

    For example:

    let number: Int? = 5
    
    // instead of
    var double: Double?
    if let number = number {
       double = Double(number)
    }
    
    // use
    let double = number?.double
    

    Declaration

    Swift

    public var double: Double { get }
  • Convert self to a Float.

    Most useful when operating on Int optionals.

    For example:

    let number: Int? = 5
    
    // instead of
    var float: Float?
    if let number = number {
       float = Float(number)
    }
    
    // use
    let float = number?.float
    

    Declaration

    Swift

    public var float: Float { get }
  • Convert self to a CGFloat.

    Most useful when operating on Int optionals.

    For example:

    let number: Int? = 5
    
    // instead of
    var cgFloat: CGFloat?
    if let number = number {
       cgFloat = CGFloat(number)
    }
    
    // use
    let cgFloat = number?.cgFloat
    

    Declaration

    Swift

    public var cgFloat: CGFloat { get }
  • Convert self to a String.

    Most useful when operating on Int optionals.

    For example:

    let number: Int? = 5
    
    // instead of
    var string: String?
    if let number = number {
       string = String(number)
    }
    
    // use
    let string = number?.string
    

    Declaration

    Swift

    public var string: String { get }
  • Convert self to an abbreviated String.

    Examples:

    Value : 598 -> 598
    Value : -999 -> -999
    Value : 1000 -> 1K
    Value : -1284 -> -1.3K
    Value : 9940 -> 9.9K
    Value : 9980 -> 10K
    Value : 39900 -> 39.9K
    Value : 99880 -> 99.9K
    Value : 399880 -> 0.4M
    Value : 999898 -> 1M
    Value : 999999 -> 1M
    Value : 1456384 -> 1.5M
    Value : 12383474 -> 12.4M
    

    Declaration

    Swift

    public var abbreviatedString: String { get }
  • Repeat a block self times.

    Declaration

    Swift

    public func `repeat`(_ block: (Int) throws -> Void) rethrows

    Parameters

    block

    The block to execute (includes the current execution index)

  • Generate a random Int bounded by a closed interval range.

    Declaration

    Swift

    public static func random(_ range: ClosedRange<Int>) -> Int
  • Generate a random Int bounded by a range from min to max.

    Declaration

    Swift

    public static func random(min: Int, max: Int) -> Int