How to find out the max value for Int in Swift
Solution 1
“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.”
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/in/jEUH0.
Solution 2
Try this in Swift 3:
let value = Int.max
Solution 3
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.
Solution 4
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.
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.
More on how Apple address the issue of integer overflows here.
thndrkiss
am a beginner ! "Time is a companion that goes with us on a journey. It reminds us to cherish each moment, because it will never come again. What we leave behind is not as important as how we have lived." - Jean Luc Picard
Updated on July 09, 2022Comments
-
thndrkiss almost 2 years
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 : 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 } }
-
thndrkiss almost 10 yearsThanks i got it, here is a query, why should it always be with the class name ? can't a instance, that is say "let intTemp : Int = 56;" can't i access intTemp.min or intTemp.max ? why it is not allowing me to do so ?
-
iPatel almost 10 years@thndrkiss - This is very new language for all of us so honestly I don't know why we does not able to use instance (such like
intTemp.max
) . But here we only accept it as design/syntax of Swift Language. -
Inder Kumar Rathore almost 10 years@thndrkiss I haven't downloaded the new sdk but it seems that
min
ormax
could be static property