日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

「Swift」笔记第一章:The Basic

發布時間:2024/9/27 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 「Swift」笔记第一章:The Basic 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 第一章:基礎

  • 提供C/OC所有基礎數據類型:int,double,float,string,bool;還提供三種基礎存儲數據的結構:array,set,dict,詳見Collection Types
  • 引入了元組
  • 引入可選類型
  • 比較安全的語言,可以幫助明確代碼可以使用的值的類型

1.1 常量和變量

  • 變量類型分為常量和變量,常量一旦設定不可改變;
  • 變量可改變(改變為同數據類型的不同內容)
  • 1.1.1 聲明常量和變量

  • 可以同時聲明多個變量:var x = 0.0, y = 0.0, z = 0.0
  • 1.1.2 類型注釋

  • List itemvar welcomeMessage: String 一旦變量不聲明數據類型但被賦值,或者聲明數據類型,那么該變量就不能存儲其他數據類型;除非使用let/var重新聲明
  • 1.1.3 命名變量和常量

  • 常量和變量可以命名為任何數據類型let π = 3.14159 let 你好 = "你好世界" let 🐶🐮 = "dogcow" 不能包含空格字符、數學符號、箭頭、專用 Unicode 標量值或線條和方框圖字符
  • 1.1.4 打印常量和變量

  • 利用print來打印print(_:separator:terminator:) print("The current value of friendlyWelcome is \(friendlyWelcome)") 其中separater和terminator是默認值,也可以修改
  • 1.2 注釋

    • //
    • /* */
    • /* /* (內嵌注釋)*/ */

    1.3 分號作用

    分割同一行中的多條命令:

    let cat = "🐱"; print(cat)

    1.4 整數

  • 帶符號/不帶符號類型
  • 均有8, 16, 32, 和 64 bit 形式
    Uint8 / Int32 等
  • 整數自帶.min和.max性質:
    以查看該類型能夠接收的數字的范圍let minValue = UInt8.min // minValue is equal to 0, and is of type UInt8 let maxValue = UInt8.max // maxValue is equal to 255, and is of type UInt8 這些屬性(UInt8.min)是適當大小的值,因此可以與同類型的其他數據使用在表達式中
  • 1.4.1 帶符號整數:Int

  • 一般Int類型跟隨系統位數,64位Int為64bit
  • 1.4.2 無符號整數:Uint

    1.5 浮點數

  • 浮點數包括 32bit 的 Float 和 64bit 的 Double
  • 1.6 類型安全和類型推斷

  • swift是類型安全的語言,數據之間的傳遞不會導致不同類型數據傳遞
  • swift使用類型推斷工具(type inference)來自動推斷變量類型var meaningOfLife = 42 // 自動推斷為帶符號整數,若要修改,只能被賦值帶符號整數值
  • 類型推斷自動推斷浮點數為float
  • 1.7 數字文本

    A decimal number, with no prefix
    A binary number, with a 0b prefix
    An octal number, with a 0o prefix
    A hexadecimal number, with a 0x prefix

    let decimalInteger = 17 let binaryInteger = 0b10001 // 17 in binary notation let octalInteger = 0o21 // 17 in octal notation let hexadecimalInteger = 0x11 // 17 in hexadecimal notation let decimalDouble = 12.1875 let exponentDouble = 1.21875e1 let hexadecimalDouble = 0xC.3p0 let paddedDouble = 000123.456 let oneMillion = 1_000_000 let justOverOneMillion = 1_000_000.000_000_1

    1.8 數據類型轉換

  • 不能對變量賦予超過其表示范圍的值
  • 不能對已知數據類型變量賦予另一類型的數據
  • 1.8.1 整數轉換

    • 可在表達式中對數據做類型轉換let twoThousand: UInt16 = 2_000 let one: UInt8 = 1 let twoThousandAndOne = twoThousand + UInt16(one)

    1.8.2 整數和浮點數的轉換

    let three = 3 let pointOneFourOneFiveNine = 0.14159 let pi = Double(three) + pointOneFourOneFiveNine // pi equals 3.14159, and is inferred to be of type Doublelet integerPi = Int(pi) // integerPi equals 3, and is inferred to be of type Int // 正負數均往零方向取整 let integerPi = Int(pi) // integerPi equals 3, and is inferred to be of type Int

    1.9 類型別名

    使用關鍵詞typealias對已存在的類型賦予別名

    typealias AudioSample = UInt16

    有助于上下文理解

    1.10 布爾值

    let orangesAreOrange = true let turnipsAreDelicious = false
    • swift語言的類型安全不允許布爾值的隱式轉換:
    let i = 1 if i {// this example will not compile, and will report an error }

    但可以通過執行判斷語句,由其返回的true/false來判斷是否進入條件

    1.11 元組

    元組可以將不同數據類型的數據放在一起:

    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"// 還可以通過下標的方式取元組中的值 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"// 還可以給元組的每個元素添加自定義描述 let http200Status = (statusCode: 200, description: "OK") print("The status code is \(http200Status.statusCode)") // Prints "The status code is 200" print("The status message is \(http200Status.description)") // Prints "The status message is OK"

    1.12 可選項(Optionals)

    當一個傳遞來的值可能是缺失時,可以用optional進行存儲,optional表示接收的值存在/不存在
    例如,當對字符串轉整數時,可能會轉換失敗:

    let possibleNumber = "123" let convertedNumber = Int(possibleNumber) // convertedNumber is inferred to be of type "Int?", or "optional Int"

    此時convertedNumber的數據類型是“Int?”,而不是Int

    1.12.1 nil

    • var serverResponseCode: Int? = 404 // serverResponseCode contains an actual Int value of 404 serverResponseCode = nil // serverResponseCode now contains no value

      變量或非可選常量不能被賦值為nil,所以當一個變量存儲的值可能缺失時,一定要用optinal

    • 如果帶optional的變量沒有初始化,那么系統會自動賦予nill:

      var surveyAnswer: String? // surveyAnswer is automatically set to nil

    1.12.2 if聲明和強制展開

    • 可以用if表達式判斷變量中是否有值:if convertedNumber != nil {print("convertedNumber contains some integer value.") } // Prints "convertedNumber contains some integer value." 如果有,可以通過!調用:if convertedNumber != nil {print("convertedNumber has an integer value of \(convertedNumber!).") } // Prints "convertedNumber has an integer value of 123." 此時如果convertedNumber含有值,由于convertedNumber是optinal類型,則用!對convertedNumber中的值進行強制提取(forced Unwrapping)

    1.12.3 可選綁定(Optinal Binding)

    • 通過可選綁定,可以查找一個optianl值是否含有實值,如果有,則將其含有的值作為臨時常量或者變量??梢酝ㄟ^if或while判斷optinal是否含有值和提取:

      if let constantName = someOptional {statements }

      利用Optinal的優勢,例如:

      let possibleNumber = "123"if let actualNumber = Int(possibleNumber) {print("The string \"\(possibleNumber)\" has an integer value of \(actualNumber)") } else {print("The string \"\(possibleNumber)\" couldn't be converted to an integer") } // Prints "The string "123" has an integer value of 123"

      也可以將actualNumber用變量(var)的方式進行申明

      optinal和if的表達式還可以結合使用,if條件判斷中的并列表達式bool結果取交集

      if let firstNumber = Int("4"), let secondNumber = Int("42"), firstNumber < secondNumber && secondNumber < 100 {print("\(firstNumber) < \(secondNumber) < 100") } // Prints "4 < 42 < 100"if let firstNumber = Int("4") {if let secondNumber = Int("42") {if firstNumber < secondNumber && secondNumber < 100 {print("\(firstNumber) < \(secondNumber) < 100")}} }

    1.12.4 隱式展開的可選項

    • 當經過程序某一過程后,一個optinal值在后續的程序中可以確定一定包含實值時,可以將optinal確認是否含值和展開過程省略。下面的代碼顯示了optinal和optinal隱式展開的區別:

      let possibleString: String? = "An optional string." // 聲明一個可選項 let forcedString: String = possibleString! // 將可選項內的值強制解包,賦給forcedString,不加!會報錯,此時forcedString的類型為Stringlet assumedString: String! = "An implicitly unwrapped optional string." // 聲明一個隱式展開的可選項 let implicitString: String = assumedString // 不再需要!來強制解包let optionalString = assumedString // 如果申明變量時沒有指出optinalString的類型,則被賦值時的類型還是optinal // The type of optionalString is "String?" and assumedString isn't force-unwrapped.

    1.13 錯誤處理

    使用錯誤處理來應對程序中可能出現的錯誤情況

    首先需要在定義函數時,使用關鍵字throw表明一個函數可能報錯:

    func canThrowAnError() throws {// this function may or may not throw an error }

    然后使用do{}catch{}...catch{}..結構來捕捉函數可能出現的錯誤:

    do {try canThrowAnError()// no error was thrown } catch {// an error was thrown } catch {... } ... // 一個例子: func makeASandwich() throws {// ... }do {try makeASandwich()eatASandwich() } catch SandwichError.outOfCleanDishes {washDishes() } catch SandwichError.missingIngredients(let ingredients) {buyGroceries(ingredients) }

    1.14 斷言和先決條件

    斷言和先決條件是程序運行過程中的檢查手段,用于確保程序執行前,判斷基本條件是否滿足,假設滿足則返回true,繼續執行之后的程序;如果不滿足,則終止程序/app的運行。

    總結

    以上是生活随笔為你收集整理的「Swift」笔记第一章:The Basic的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。