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

时间:2022-06-01 20:39:55

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 ?

我想知道如何访问“struct”类型的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 }
}

4 个解决方案

#1


53  

“You can access the minimum and maximum values of each integer type with its min and max properties:

“您可以使用每个整数类型的最小值和最大值来访问它的最小值和最大值。

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.

摘自:苹果公司“Swift编程语言”。“iBooks。https://itun.es/in/jEUH0。

#2


4  

You can access the minimum and maximum values of each integer type with its min and max properties:

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

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),因此可以与相同类型的其他值一起使用。

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

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

#3


0  

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.

更多关于苹果如何解决整数溢出的问题。

#4


0  

Try this in Swift 3:

试一下Swift 3:

let value = Int.max

#1


53  

“You can access the minimum and maximum values of each integer type with its min and max properties:

“您可以使用每个整数类型的最小值和最大值来访问它的最小值和最大值。

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.

摘自:苹果公司“Swift编程语言”。“iBooks。https://itun.es/in/jEUH0。

#2


4  

You can access the minimum and maximum values of each integer type with its min and max properties:

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

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),因此可以与相同类型的其他值一起使用。

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

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

#3


0  

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.

更多关于苹果如何解决整数溢出的问题。

#4


0  

Try this in Swift 3:

试一下Swift 3:

let value = Int.max