ios 如何在 Swift 中找出 Int 的最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24030606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find out the max value for Int in Swift
提问by thndrkiss
I want to understand how to access the "struct" type of Int. When I cmd-clicked Int it took me to this class, i want to find out what is the maximum value this can hold. Is there a way to pull from one of this properties ?. what is max and min in this structure ?
我想了解如何访问 Int 的“结构”类型。当我 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 }
}
回答by iPatel
“You can access the minimum and maximum values of each integer type with its min and max properties:
“您可以使用其 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
The values of these properties are of the appropriate-sized number type (such as UInt8 in the example above) and can therefore be used in expressions alongside other values of the same type.”
这些属性的值是适当大小的数字类型(例如上面示例中的 UInt8),因此可以与相同类型的其他值一起用于表达式中。”
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/in/jEUH0.
摘自:Apple Inc. “The Swift Programming Language”。电子书。https://itun.es/in/jEUH0。
回答by Shadros
Try this in Swift 3:
在 Swift 3 中试试这个:
let value = Int.max
回答by Artem Z.
You can access the minimum and maximum values of each integer type with its min and max properties:
您可以使用其 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
The values of these properties are of the appropriate-sized number type (such as UInt8 in the example above) and can therefore be used in expressions alongside other values of the same type.
这些属性的值是适当大小的数字类型(例如上面示例中的 UInt8),因此可以在表达式中与相同类型的其他值一起使用。
来自:https: //developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html
回答by joakim
You can access these as static properties as suggested in other answers.
您可以按照其他答案中的建议将这些作为静态属性访问。
I see in some comments people are wondering why you can't access them as instance variables.
我在一些评论中看到人们想知道为什么不能将它们作为实例变量访问。
This is like asking "what is the max value of 5?" and expecting a sensible answer.
这就像问“5 的最大值是多少?” 并期待一个明智的答案。
One of the main uses for these variables is guarding against integer overflows.
这些变量的主要用途之一是防止整数溢出。
Something along the lines of "if I add something to this integer that makes it bigger than Int.max i.e. it triggers an overflow " and act accordingly.
类似于“如果我向该整数添加一些内容使其大于 Int.max 的内容,即它会触发溢出”并相应地采取行动。
More on how Apple address the issue of integer overflows here.
有关 Apple 如何解决整数溢出问题的更多信息,请点击此处。