Swift: Basics
Basics — A new programming language for iOS, macOS, watchOS, and tvOS app development.
Additional information
- provides its own versions of all fundamental C and Objective-C types, including Int for integers, Double and Float for floating-point values, Bool for Boolean values, and String for textual data.
- Provides powerful versions of the three primary collection types, Array, Set, and Dictionary.
- Is a type-safe language, which means the language helps you to be clear about the types of values your code can work with.
- Introduces optional types, which handle the absence of a value.
Constants and Variables
Type Annotations
Additional information
Printing Constants and Variables
Additional information
Comments
Additional information
Integers
- Are either signed (positive, zero, or negative) or unsigned (positive or zero).
- Swift provides signed and unsigned integers in 8, 16, 32, and 64 bit forms.
- These follow a naming convention similar to C, in that an 8-bit unsigned integer is of type UInt8, and a 32-bit signed integer is of type Int32.
Additional information
Floating-Point Numbers
Type Safety and Type Inference
If you don’t specify the type of value you need, Swift uses type inference to work out the appropriate type.
Enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.
Is particularly useful when you declare a constant or variable with an initial value.
Additional information
- Because Swift is type safe, it performs type checks when compiling your code and flags any mismatched types as errors. .
- Type-checking helps you avoid errors when you’re working with different types of values. .
- Swift always chooses Double (rather than Float) when inferring the type of floating-point numbers.
Numeric Literals
Let decimalInteger = 17
let binaryInteger = 0b10001 // 17 in binary notation
let octalInteger = 0o21 // 17 in octal notation
let hexadecimalInteger = 0x11 // 17 in hexadecimal notation Floating-point literals can be decimal (with no prefix), or hexadecimal (with a 0x prefix).
Additional information
Numeric Type Conversion
Use the Int type for all general-purpose integer constants and variables in your code, even if they’re known to be nonnegative.
Use other integer types only when they’re specifically needed for the task at hand, because of explicitly sized data from an external source, or for performance, memory usage, or other necessary optimization.
Type Aliases
Booleans
If turnipsAreDelicious {
print("Mmm, tasty turnips!")
} else {
print("Eww, turnips are horrible.")
}
// Prints "Eww, turnips are horrible." Swift has a basic Boolean type, called Bool.
Additional information
Tuples
Are particularly useful as the return values of functions.
Are useful for temporary groups of related values.
Let http404Error = (404, "Not Found")
// http404Error is of type (Int, String), and equals (404, "Not Found") Let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
// Prints "The status code is 404"
print("The status message is \(statusMessage)")
// Prints "The status message is Not Found" Let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// Prints "The status code is 404" let http200Status = (statusCode: 200, description: "OK")
Print("The status code is \(http404Error.0)")
// Prints "The status code is 404"
print("The status message is \(http404Error.1)")
// Prints "The status message is Not Found" Additional information
Optionals
Let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// convertedNumber is inferred to be of type "Int?", or "optional Int" Use optionals in situations where a value may be absent.
Represents two possibilities: Either there is a value, and you can unwrap the optional to access that value, or there isn’t a value at all.
Error Handling
You use error handling to respond to error conditions your program may encounter during execution.
Allows you to determine the underlying cause of failure, and, if necessary, propagate the error to another part of your program.
Assertions and Preconditions
Related concepts
- Basics: Constants and Variables
- Basics: Type Annotations
- Basics: Printing Constants and Variables
- Basics: Comments
- Basics: Floating-Point Numbers
- Basics: Type Safety and Type Inference
- Basics: Numeric Literals
- Basics: Numeric Type Conversion
- Basics: Type Aliases
- Basics: Booleans
- Basics: Tuples
- Basics: Optionals
- Basics: Error Handling
- Basics: Assertions and Preconditions
Semantic portal