Swift: Type Safety and Conversions
Type Safety and Type Inference
Swift is a type-safe language. This means that the compiler checks to make sure that you are only using variables and constants in the ways that they were intended.Swift also supports type inference. This means that the compiler can infer the type of a variable or constant based on the value that you assign to it.
For example, the following code shows how to use type inference:
let number = 10The compiler can infer that the variable
number is an integer based on the value that is assigned to it.Numeric Literals
Numeric literals are numbers that are written in the code itself.For example, the following code shows some numeric literals:
let integerLiteral = 10let floatLiteral = 3.14159let doubleLiteral = 3.14159Numeric Type Conversion
Numeric type conversion is the process of converting one numeric type to another.For example, the following code shows how to convert an integer to a double:
let integer: Int = 10let double: Double = Double(integer)Integer Conversion
Integer conversion is the process of converting one integer type to another.For example, the following code shows how to convert an
Int to a UInt:let integer: Int = 10let unsignedInteger: UInt = UInt(integer)Integer and Floating-Point Conversion
Integer and floating-point conversion is the process of converting between integer and floating-point types.For example, the following code shows how to convert an
Int to a Double:let integer: Int = 10let double: Double = Double(integer)| Previous: Comments, Integers, Floating Point Numbers | Next: Type Aliases, Booleans, Tuples |
Comments
Post a Comment