# Swift语言

## 条件控制

Another way to handle optional values is to provide a default value using the ?? operator. If the optional value is missing, the default value is used instead.

Switches support any kind of data and a wide variety of comparison operations—they aren’t limited to integers and tests for equality.

After executing the code inside the switch case that matched, the program exits from the switch statement. Execution doesn’t continue to the next case, so you don’t need to explicitly break out of the switch at the end of each case’s code.

You use for-in to iterate over items in a dictionary by providing a pair of names to use for each key-value pair. Dictionaries are an unordered collection, so their keys and values are iterated over in an arbitrary order.

Use while to repeat a block of code until a condition changes. The condition of a loop can be at the end instead, ensuring that the loop is run at least once.

You can keep an index in a loop by using ..< to make a range of indexes.

Use ..< to make a range that omits its upper value, and use ... to make a range that includes both values.

## 函数和方法

By default, functions use their parameter names as labels for their arguments. Write a custom argument label before the parameter name, or write _ to use no argument label.

Use a tuple to make a compound value—for example, to return multiple values from a function. The elements of a tuple can be referred to either by name or by number.

Functions can be nested. Nested functions have access to variables that were declared in the outer function. You can use nested functions to organize the code in a function that’s long or complex.

Functions are a first-class type. This means that a function can return another function as its value.

A function can take another function as one of its arguments.

Functions are actually a special case of closures: blocks of code that can be called later. The code in a closure has access to things like variables and functions that were available in the scope where the closure was created, even if the closure is in a different scope when it’s executed—you saw an example of this already with nested functions. You can write a closure without a name by surrounding code with braces ({}). Use in to separate the arguments and return type from the body.

map: Returns an array containing the results of mapping the given closure over the sequence’s elements.
Declarationfunc map<T>(_ transform: (Int) throws -> T) rethrows -> [T]
DiscussionIn this example, map is used first to convert the names in the array to lowercase strings and then to count their characters.let cast = ["Vivien", "Marlon", "Kim", "Karl"] let lowercaseNames = cast.map { $0.lowercased() } // 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"] let letterCounts = cast.map {$0.count } // 'letterCounts' == [6, 6, 3, 4]
ParameterstransformA mapping closure. transform accepts an element of this sequence as its parameter and returns a transformed value of the same or of a different type.-No description.
ReturnsAn array containing the transformed elements of this sequence.

You have several options for writing closures more concisely. When a closure’s type is already known, such as the callback for a delegate, you can omit the type of its parameters, its return type, or both. Single statement closures implicitly return the value of their only statement.

You can refer to parameters by number instead of by name—this approach is especially useful in very short closures. A closure passed as the last argument to a function can appear immediately after the parentheses. When a closure is the only argument to a function, you can omit the parentheses entirely.

## 对象与类

Use class followed by the class’s name to create a class. A property declaration in a class is written the same way as a constant or variable declaration, except that it’s in the context of a class. Likewise, method and function declarations are written the same way.

Create an instance of a class by putting parentheses after the class name. Use dot syntax to access the properties and methods of the instance.

Use init:

Override:

getter and a setter:

If you don’t need to compute the property but still need to provide code that’s run before and after setting a new value, use willSet and didSet. The code you provide is run any time the value changes outside of an initializer. For example, the class below ensures that the side length of its triangle is always the same as the side length of its square.

Optional value:

## 枚举和结构体

Use enum to create an enumeration. Like classes and all other named types, enumerations can have methods associated with them.

By default, Swift assigns the raw values starting at zero and incrementing by one each time, but you can change this behavior by explicitly specifying values. In the example above, ace is explicitly given a raw value of 1, and the rest of the raw values are assigned in order. You can also use strings or floating-point numbers as the raw type of an enumeration. Use the rawValue property to access the raw value of an enumeration case.

Use the init?(rawValue:) initializer to make an instance of an enumeration from a raw value. It returns either the enumeration case matching the raw value or nil if there’s no matching Rank.

Summary: A half-open interval from a lower bound up to, but not including, an upper bound.
Declarationstruct Range<Bound> where Bound : Comparable
DiscussionYou create a Range instance by using the half-open range operator (..<).let underFive = 0.0..<5.0You can use a Range instance to quickly check if a value is contained in a particular range of values. For example:underFive.contains(3.14) // true underFive.contains(6.28) // false underFive.contains(5.0) // falseRange instances can represent an empty interval, unlike ClosedRange.let empty = 0.0..<0.0 empty.contains(0.0) // false empty.isEmpty // trueUsing a Range as a Collection of Consecutive ValuesWhen a range uses integers as its lower and upper bounds, or any other type that conforms to the Strideable protocol with an integer stride, you can use that range in a for-in loop or with any sequence or collection method. The elements of the range are the consecutive values from its lower bound up to, but not including, its upper bound.for n in 3..<5 { print(n) } // Prints "3" // Prints "4"Because floating-point types such as Float and Double are their own Stride types, they cannot be used as the bounds of a countable range. If you need to iterate over consecutive floating-point values, see the stride(from:to:by:) function.

Most app UIs are very visual experiences, so most accessibility work focuses on VoiceOver — a screen reader that lets people with low to no vision use Apple devices without needing to see their screens. VoiceOver reads out information to users about your app’s UI elements. It’s up to you to make sure this information helps users interact efficiently with your app.