Tutorial by Examples

Swift's built-in numeric types are: Word-sized (architecture-dependent) signed Int and unsigned UInt. Fixed-size signed integers Int8, Int16, Int32, Int64, and unsigned integers UInt8, UInt16, UInt32, UInt64. Floating-point types Float32/Float, Float64/Double, and Float80 (x86-only). Literal...
func doSomething1(value: Double) { /* ... */ } func doSomething2(value: UInt) { /* ... */ } let x = 42 // x is an Int doSomething1(Double(x)) // convert x to a Double doSomething2(UInt(x)) // convert x to a UInt Integer initializers produce a runtime error if the value ove...
Use String initializers for converting numbers into strings: String(1635999) // returns "1635999" String(1635999, radix: 10) // returns "1635999" String(1635999, radix: 2) // returns "110001111011010011111&...
round Rounds the value to the nearest whole number with x.5 rounding up (but note that -x.5 rounds down). round(3.000) // 3 round(3.001) // 3 round(3.499) // 3 round(3.500) // 4 round(3.999) // 4 round(-3.000) // -3 round(-3.001) // -3 round(-3.499) // -3 round(-3.500) // -4 *** careful...
arc4random_uniform(someNumber: UInt32) -> UInt32 This gives you random integers in the range 0 to someNumber - 1. The maximum value for UInt32 is 4,294,967,295 (that is, 2^32 - 1). Examples: Coin flip let flip = arc4random_uniform(2) // 0 or 1 Dice roll let roll = arc4random_...
In Swift, we can exponentiate Doubles with the built-in pow() method: pow(BASE, EXPONENT) In the code below, the base (5) is set to the power of the exponent (2) : let number = pow(5.0, 2.0) // Equals 25

Page 1 of 1