Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the max value for Int in Swift

Tags:

ios

struct

swift

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 }
}
like image 933
thndrkiss Avatar asked Jun 04 '14 06:06

thndrkiss


People also ask

What is int max in Swift?

Unless you need to work with a specific size of integer, always use Int for integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms, Int can store any value between -2,147,483,648 and 2,147,483,647 , and is large enough for many integer ranges.

What is the max value of int?

The int type in Java can be used to represent any whole number from -2147483648 to 2147483647.

How do you find the highest number in Swift?

To find the largest number in an array in Swift, call max() method. max() method returns the maximum of the elements present in the array.


4 Answers

“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.

like image 146
iPatel Avatar answered Oct 14 '22 00:10

iPatel


Try this in Swift 3:

let value = Int.max
like image 28
Shadros Avatar answered Oct 14 '22 01:10

Shadros


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.

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

like image 29
Artem Z. Avatar answered Oct 14 '22 02:10

Artem Z.


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.

like image 43
joakim Avatar answered Oct 14 '22 01:10

joakim