|
Numbers
|
val one = 1 // Int
val threeBillion = 3000000000 // Long
val oneLong = 1L // Long
val oneByte: Byte = 1
|
let one = 1 // Int
let threeBillion = 3000000000 //Int (Int64)
let oneDouble = 1.0 // Double
let oneByte: UInt8 = 1
|
val pi = 3.14 // Double
val e = 2.7182818284 // Double
val eFloat = 2.7182818284f // Float, actual value is 2.7182817
|
let pi = 3.14 // Double
let e = 2.7182818284 // Double
let eFloat: Float = 2.7182818284 // Float, actual value is 2.71828175
|
fun main() {
fun printDouble(d: Double) { print(d) }
val i = 1
val d = 1.1
val f = 1.1f
printDouble(d)
// printDouble(i) // Error: Type mismatch
// printDouble(f) // Error: Type mismatch
}
|
func main() {
func printDouble(_ d: Double) { print(d) }
let i = 1
let d = 1.1
let f: Float = 1.1
printDouble(d)
// printDouble(i) // Error: Cannot convert value of type 'Int' to expected argument type 'Double'
// printDouble(f) // Error: Cannot convert value of type 'Float' to expected argument type 'Double'
}
|
Underscores in numeric literals (since 1.1)
|
val oneMillion = 1_000_000
val creditCardNumber = 1234_5678_9012_3456L
val socialSecurityNumber = 999_99_9999L
val hexBytes = 0xFF_EC_DE_5E
val bytes = 0b11010010_01101001_10010100_10010010
|
let oneMillion = 1_000_000
let creditCardNumber = 1234_5678_9012_3456
let socialSecurityNumber = 999_99_9999
let hexBytes = 0xFF_EC_DE_5E
let bytes = 0b11010010_01101001_10010100_10010010
|
Representation
|
val a: Int = 100
val boxedA: Int? = a
val anotherBoxedA: Int? = a
val b: Int = 10000
val boxedB: Int? = b
val anotherBoxedB: Int? = b
println(boxedA === anotherBoxedA) // true
println(boxedB === anotherBoxedB) // false
|
let a: Int = 100
let boxedA: Int? = a
let anotherBoxedA: Int? = a
let b: Int = 10000
let boxedB: Int? = b
let anotherBoxedB: Int? = b
print(boxedA == anotherBoxedA) // true
print(boxedB == anotherBoxedB) // true
|
val a: Int = 10000
println(a == a) // Prints 'true'
val boxedA: Int? = a
val anotherBoxedA: Int? = a
println(boxedA == anotherBoxedA) // Prints 'true'
|
let a: Int = 10000
print(a == a) // Prints 'true'
let boxedA: Int? = a
let anotherBoxedA: Int? = a
print(boxedA == anotherBoxedA) // Prints 'true'
|
Explicit conversions
|
// Hypothetical code, does not actually compile:
val a: Int? = 1 // A boxed Int (java.lang.Integer)
val b: Long? = a // implicit conversion yields a boxed Long (java.lang.Long)
print(b == a) // Surprise! This prints "false" as Long's equals() checks whether the other is Long as well
|
let a: Int? = 1 // Optional
let b: Double? = Double(a!)
print(b == a) //Error: Binary operator '==' cannot be applied to operands of type 'Double?' and 'Int?'
|
val b: Byte = 1 // OK, literals are checked statically
val i: Int = b // ERROR
|
let b: Int8 = 1 // OK, literals are checked statically
let i: Int = b // ERROR
|
val i: Int = b.toInt() // OK: explicitly widened
print(i)
|
let i: Int? = Int(b)
print(i)
|
val l = 1L + 3 // Long + Int => Long
|
let l = 1.0 + 3 // Double + Int => Double
|
Division of integers
|
val x = 5 / 2
//println(x == 2.5) // ERROR: Operator '==' cannot be applied to 'Int' and 'Double'
println(x == 2)
|
let x = 5 / 2 //x is Int
//print(x == 2.5) // ERROR: error: cannot convert value of type 'Double' to expected argument type 'Int'
print(x == 2)
|
val x = 5L / 2
println(x == 2L)
|
let x = 5.0 / 2
print(x == 2.0) //false
|
val x = 5 / 2.toDouble()
println(x == 2.5)
|
let x = 5 / 2.0
print(x == 2.5) //true
|
Bitwise operations
|
val x = (1 shl 2) and 0x000FF000
|
let x = (1 << 2) & 0x000FF000
|
Characters
|
fun check(c: Char) {
if (c == 1) { // ERROR: incompatible types
// ...
}
}
|
func check(c: Character) {
if c == 1 { // ERROR: incompatible types
// ...
}
}
|
fun decimalDigitValue(c: Char): Int {
if (c !in '0'..'9')
throw IllegalArgumentException("Out of range")
return c.toInt() - '0'.toInt() // Explicit conversions to numbers
}
|
enum Error: Swift.Error {
case illegalArgumentException
}
func decimalDigitValue(c: Character) throws -> Int? {
guard let int = Int(String(c)), 0...9 ~= int else {
throw Error.illegalArgumentException
}
return int
}
|
Arrays
|
class Array<T> private constructor() {
val size: Int
operator fun get(index: Int): T
operator fun set(index: Int, value: T): Unit
operator fun iterator(): Iterator<T>
// ...
}
|
struct Array<Element> {
private init() {}
let size: Int
func get(index: Int) -> Element { /*...*/ }
func set(index: Int, value: Element) { /*...*/ }
func iterator() -> Iterator<Element> { /*...*/ }
// ...
}
|
// Creates an Array<String> with values ["0", "1", "4", "9", "16"]
val asc = Array(5) { i -> (i * i).toString() }
asc.forEach { println(it) }
|
// Creates an Array<String> with values ["0", "1", "4", "9", "16"]
let asc = (0..<5).map { String($0 * $0) }
asc.forEach { print($0) }
|
Primitive type arrays
|
val x: IntArray = intArrayOf(1, 2, 3)
x[0] = x[1] + x[2]
|
var x = [1, 2, 3]
x[0] = x[1] + x[2]
|
// Array of int of size 5 with values [0, 0, 0, 0, 0]
val arr = IntArray(5)
// e.g. initialise the values in the array with a constant
// Array of int of size 5 with values [42, 42, 42, 42, 42]
val arr = IntArray(5) { 42 }
// e.g. initialise the values in the array using a lambda
// Array of int of size 5 with values [0, 1, 2, 3, 4] (values initialised to their index value)
var arr = IntArray(5) { it * 1 }
|
// Array of int of size 5 with values [0, 0, 0, 0, 0]
let arr = Array(repeating: 0, count: 5)
// e.g. initialise the values in the array with a constant
// Array of int of size 5 with values [42, 42, 42, 42, 42]
let arr = Array(repeating: 42, count: 5)
// e.g. initialise the values in the array using a lambda
// Array of int of size 5 with values [0, 1, 2, 3, 4] (values initialised to their index value)
var arr = (0..<5).map { $0 }
|
Literals
|
val b: UByte = 1u // UByte, expected type provided
val s: UShort = 1u // UShort, expected type provided
val l: ULong = 1u // ULong, expected type provided
val a1 = 42u // UInt: no expected type provided, constant fits in UInt
val a2 = 0xFFFF_FFFF_FFFFu // ULong: no expected type provided, constant doesn't fit in UInt
|
let b: UInt8 = 1 // UInt8, expected type provided
let s: UInt32 = 1 // UInt32, expected type provided
let l: UInt64 = 1 // UInt64, expected type provided
let a1: UInt = 42 // UInt, expected type provided
let a2: UInt64 = 0xFFFF_FFFF_FFFF // UInt64, expected type provided
|
val a = 1UL // ULong, even though no expected type provided and constant fits into UInt
|
let a: UInt64 = 1
|
Strings
|
for (c in str) {
println(c)
}
|
for c in str {
print(c)
}
|
val s = "abc" + 1
println(s + "def")
|
let s = "abc" + String(1)
print(s + "def")
|
String literals
|
val s = "Hello, world!\n"
|
let s = "Hello, world!\n"
|
val text = """
for (c in "foo")
print(c)
"""
|
let text = """
for (c in "foo")
print(c)
"""
|
val text = """
|Tell me and I forget.
|Teach me and I remember.
|Involve me and I learn.
|(Benjamin Franklin)
""".trimMargin()
|
let text = """
Tell me and I forget.
Teach me and I remember.
Involve me and I learn.
(Benjamin Franklin)
"""
|
String templates
|
val i = 10
println("i = $i") // prints "i = 10"
|
let i = 10
print("i = \(i)") // prints "i = 10"
|
val s = "abc"
println("$s.length is ${s.length}") // prints "abc.length is 3"
|
let s = "abc"
print("\(s).count is \(s.count)") // prints "abc.count is 3"
|
val price = """
${'$'}9.99
"""
|
let price = """
$9.99
"""
|