first


https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html

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.
  1. let nickName: String? = nil
  2. let fullName: String = "John Appleseed"
  3. let informalGreeting = "Hi \(nickName ?? fullName)"
Switches support any kind of data and a wide variety of comparison operations—they aren’t limited to integers and tests for equality.
  1. let vegetable = "red pepper"
  2. switch vegetable {
  3. case "celery":
  4. print("Add some raisins and make ants on a log.")
  5. case "cucumber", "watercress":
  6. print("That would make a good tea sandwich.")
  7. case let x where x.hasSuffix("pepper"):
  8. print("Is it a spicy \(x)?")
  9. default:
  10. print("Everything tastes good in soup.")
  11. }

Notice how let can be used in a pattern to assign the value that matched the pattern to a constant.
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 there is no 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.
  1. let interestingNumbers = [
  2. "Prime": [2, 3, 5, 7, 11, 13],
  3. "Fibonacci": [1, 1, 2, 3, 5, 8],
  4. "Square": [1, 4, 9, 16, 25],
  5. ]
  6. var largest = 0
  7. for (kind, numbers) in interestingNumbers {
  8. for number in numbers {
  9. if number > largest {
  10. largest = number
  11. }
  12. }
  13. }
  14. print(largest)

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.
  1. var n = 2
  2. while n < 100 {
  3. n *= 2
  4. }
  5. print(n)
  6. var m = 2
  7. repeat {
  8. m *= 2
  9. } while m < 100
  10. print(m)
You can keep an index in a loop by using ..< to make a range of indexes.
  1. var total = 0
  2. for i in 0..<4 {
  3. total += i
  4. }
  5. print(total)
Use ..< to make a range that omits its upper value, and use ... to make a range that includes both values.

Functions and Closures

Use func to declare a function. Call a function by following its name with a list of arguments in parentheses. Use -> to separate the parameter names and types from the function’s return type.
  1. func greet(person: String, day: String) -> String {
  2. return "Hello \(person), today is \(day)."
  3. }
  4. greet(person: "Bob", day: "Tuesday")

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.
  1. func greet(_ person: String, on day: String) -> String {
  2. return "Hello \(person), today is \(day)."
  3. }
  4. greet("John", on: "Wednesday")
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.
  1. func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
  2. var min = scores[0]
  3. var max = scores[0]
  4. var sum = 0
  5. for score in scores {
  6. if score > max {
  7. max = score
  8. } else if score < min {
  9. min = score
  10. }
  11. sum += score
  12. }
  13. return (min, max, sum)
  14. }
  15. let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9])
  16. print(statistics.sum)
  17. print(statistics.2)
  18. ==============================================================

Comments

Popular posts from this blog

UISearchController Tutorial: Getting Started

CALayer Tutorial for iOS: Getting Started

Create a gradient color view in iOS using Swift 3 and XCode 8