如何在Swift中找出Int的最大值

thn*_*iss 38 struct ios swift

我想了解如何访问Int的"struct"类型.当我cmd点击Int我带到了这个课程时,我想知道它可以容纳的最大值是多少.有没有办法从这个属性之一拉?这个结构中的最大值和最小值是多少?

struct Int : SignedInteger {
    var value: Builtin.Word
    init()
    init(_ v: Builtin.Word)
    init(_ value: Int)
    static func convertFromIntegerLiteral(value: Int) -> Int
    typealias ArrayBoundType = Int
    func getArrayBoundValue() -> Int
    static var max: Int { get }
    static var min: Int { get }
}
Run Code Online (Sandbox Code Playgroud)

iPa*_*tel 63

"您可以使用其最小和最大属性访问每个整数类型的最小值和最大值:

let minValue = UInt8.min  // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8.max  // maxValue is equal to 255, and is of type UInt8
Run Code Online (Sandbox Code Playgroud)

这些属性的值具有适当大小的数字类型(例如上面示例中的UInt8),因此可以在表达式中与其他相同类型的值一起使用."

摘录自:Apple Inc."The Swift Programming Language."iBooks.https://itun.es/in/jEUH0.

  • @thndrkiss - 这是对我们所有人的非常新的语言,所以老实说,我不知道为什么我们不能够使用实例*(例如像`intTemp.max`)*.但在这里我们只接受它作为**Swift Language**的设计/语法. (2认同)

Sha*_*ros 13

在Swift 3中试试这个:

let value = Int.max
Run Code Online (Sandbox Code Playgroud)


Art*_* Z. 6

您可以使用min和max属性访问每个整数类型的最小值和最大值:

let minValue = UInt8.min  // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8.max  // maxValue is equal to 255, and is of type UInt8
Run Code Online (Sandbox Code Playgroud)

这些属性的值具有适当大小的数字类型(例如上面示例中的UInt8),因此可以在表达式中与其他相同类型的值一起使用.

来自:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html