[swift] A Swift Tour -Simple Values


📌 Appleの公式文書「SWIFTプログラミング言語」(SWIFT 5.5)のA SWIFT Toorは、その名の通り、「クイックブラウズ」で整理された文章です.

📙Simple Values


📎let and var

var myVariable = 42
myVariable = 50
let myConstant = 42
myConstant = 50 🚫 상수 값 변경 불가능
  • タイプX→コンパイラ推定
  • タイプを常に明確に記入する必要があります
    let implicitDouble = 70.0 // explicit
    let explictDouble: Double = 50 // implicit
  • ファジイまたは初期値がない場合、明示的な表示タイプ
  • 📎Convert to another type

    let label = "The width is "
    let width = 94
    let widthLabel = label + String(width)
    //let widthLabel = label + width 🚫
    
  • 値は暗黙的に他のタイプに変換する、X→明示的に
  • に変換する.
    let apples = 3
    let oranges = 5
    let appleSummary = "I have \(apples) apples." // I have 3 apples.
    let fruitSummary = "I have \(apples + oranges) pieces of fruit." // I have 8 pieces of fruit.
  • 文字列に他のタイプをより簡単に含めるには、次の手順に従います.
    使用→(values)
  • 📎Multiple lines of string: use """

    let quotation = """
    I said "I have \(apples) apples."
    And then I said "I have \(apples + oranges) pieces of fruit."
    """

    📎Arrays and Dictionaries

  • インデックスまたはキー値によって要素
  • にアクセスする.
  • 最後の要素の後ろのカンマ,
  • であることができる.

    - Array

    let emptyArray: [String] = [] // empty array 선언
    var shoppingList = ["catfish", " water", "tulips"] // array 선언
    shoppingList.append("blue paint") //element 추가

    - Dictionary

    let emptyDictionary: [String:Float]=[:] // empty dictionary 선언
    var occupations = ["Malcolm": "Captain", "Kaylee": "Mechanic"] // dictionary 선언
    occupations["Jayne"] = "Public Relations" // element 추가