<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Swift 字典 Swift 字典用來存儲無序的相同類型數據的集合,Swift 數組會強制檢測元素的類型,如果類型不同則會報錯。 Swift 字典每個值(value)都關聯唯一的鍵(key),鍵作為字典中的這個值數據的標識符。 和數組中的數據項不同,字典中的數據項并沒有具體順序。我們在需要通過標識符(鍵)訪問數據的時候使用字典,這種方法很大程度上和我們在現實世界中使用字典查字義的方法一樣。 Swift 字典的key沒有類型限制可以是整型或字符串,但必須是唯一的。 如果創建一個字典,并賦值給一個變量,則創建的字典就是可以修改的。這意味著在創建字典后,可以通過添加、刪除、修改的方式改變字典里的項目。如果將一個字典賦值給常量,字典就不可修改,并且字典的大小和內容都不可以修改。 ## 創建字典 我們可以使用以下語法來創建一個特定類型的空字典: ``` var someDict = [KeyType: ValueType]() ``` 以下是創建一個空字典,鍵的類型為 Int,值的類型為 String 的簡單語法: ``` var someDict = [Int: String]() ``` 以下為創建一個字典的實例: ``` var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] ``` ## 訪問字典 我們可以根據字典的索引來訪問數組的元素,語法如下: ``` var someVar = someDict[key] ``` 我們可以通過以下實例來學習如何創建,初始化,訪問字典: ``` import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var someVar = someDict[1] print( "key = 1 的值為 \(someVar)" ) print( "key = 2 的值為 \(someDict[2])" ) print( "key = 3 的值為 \(someDict[3])" ) ``` 以上程序執行輸出結果為: ``` key = 1 的值為 Optional("One") key = 2 的值為 Optional("Two") key = 3 的值為 Optional("Three") ``` ## 修改字典 我們可以使用 **updateValue(forKey:)** 增加或更新字典的內容。如果 key 不存在,則添加值,如果存在則修改 key 對應的值。updateValue(_:forKey:)方法返回Optional值。實例如下: ``` import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var oldVal = someDict.updateValue("One 新的值", forKey: 1) var someVar = someDict[1] print( "key = 1 舊的值 \(oldVal)" ) print( "key = 1 的值為 \(someVar)" ) print( "key = 2 的值為 \(someDict[2])" ) print( "key = 3 的值為 \(someDict[3])" ) ``` 以上程序執行輸出結果為: ``` key = 1 舊的值 Optional("One") key = 1 的值為 Optional("One 新的值") key = 2 的值為 Optional("Two") key = 3 的值為 Optional("Three") ``` 你也可以通過指定的 key 來修改字典的值,如下所示: ``` import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var oldVal = someDict[1] someDict[1] = "One 新的值" var someVar = someDict[1] print( "key = 1 舊的值 \(oldVal)" ) print( "key = 1 的值為 \(someVar)" ) print( "key = 2 的值為 \(someDict[2])" ) print( "key = 3 的值為 \(someDict[3])" ) ``` 以上程序執行輸出結果為: ``` key = 1 舊的值 Optional("One") key = 1 的值為 Optional("One 新的值") key = 2 的值為 Optional("Two") key = 3 的值為 Optional("Three") ``` ## 移除 Key-Value 對 我們可以使用 **removeValueForKey()** 方法來移除字典 key-value 對。如果 key 存在該方法返回移除的值,如果不存在返回 nil 。實例如下: ``` import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var removedValue = someDict.removeValueForKey(2) print( "key = 1 的值為 \(someDict[1])" ) print( "key = 2 的值為 \(someDict[2])" ) print( "key = 3 的值為 \(someDict[3])" ) ``` 以上程序執行輸出結果為: ``` key = 1 的值為 Optional("One") key = 2 的值為 nil key = 3 的值為 Optional("Three") ``` 你也可以通過指定鍵的值為 nil 來移除 key-value(鍵-值)對。實例如下: ``` import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] someDict[2] = nil print( "key = 1 的值為 \(someDict[1])" ) print( "key = 2 的值為 \(someDict[2])" ) print( "key = 3 的值為 \(someDict[3])" ) ``` 以上程序執行輸出結果為: ``` key = 1 的值為 Optional("One") key = 2 的值為 nil key = 3 的值為 Optional("Three") ``` ## 遍歷字典 我們可以使用 **for-in** 循環來遍歷某個字典中的鍵值對。實例如下: ``` import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] for (key, value) in someDict { print("字典 key \(key) - 字典 value \(value)") } ``` 以上程序執行輸出結果為: ``` 字典 key 2 - 字典 value Two 字典 key 3 - 字典 value Three 字典 key 1 - 字典 value One ``` 我們也可以使用enumerate()方法來進行字典遍歷,返回的是字典的索引及 (key, value) 對,實例如下: ``` import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] for (key, value) in someDict.enumerate() { print("字典 key \(key) - 字典 (key, value) 對 \(value)") } ``` 以上程序執行輸出結果為: ``` 字典 key 0 - 字典 (key, value) 對 (2, "Two") 字典 key 1 - 字典 (key, value) 對 (3, "Three") 字典 key 2 - 字典 (key, value) 對 (1, "One") ``` ## 字典轉換為數組 你可以提取字典的鍵值(key-value)對,并轉換為獨立的數組。實例如下: ``` import Cocoa var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"] let dictKeys = [Int](someDict.keys) let dictValues = [String](someDict.values) print("輸出字典的鍵(key)") for (key) in dictKeys { print("\(key)") } print("輸出字典的值(value)") for (value) in dictValues { print("\(value)") } ``` 以上程序執行輸出結果為: ``` 輸出字典的鍵(key) 2 3 1 輸出字典的值(value) Two Three One ``` ## count 屬性 我們可以使用只讀的 **count** 屬性來計算字典有多少個鍵值對: ``` import Cocoa var someDict1:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var someDict2:[Int:String] = [4:"Four", 5:"Five"] print("someDict1 含有 \(someDict1.count) 個鍵值對") print("someDict2 含有 \(someDict2.count) 個鍵值對") ``` 以上程序執行輸出結果為: ``` someDict1 含有 3 個鍵值對 someDict2 含有 2 個鍵值對 ``` ## isEmpty 屬性 Y我們可以通過只讀屬性 **isEmpty** 來判斷字典是否為空,返回布爾值: ``` import Cocoa var someDict1:[Int:String] = [1:"One", 2:"Two", 3:"Three"] var someDict2:[Int:String] = [4:"Four", 5:"Five"] var someDict3:[Int:String] = [Int:String]() print("someDict1 = \(someDict1.isEmpty)") print("someDict2 = \(someDict2.isEmpty)") print("someDict3 = \(someDict3.isEmpty)") ``` 以上程序執行輸出結果為: ``` someDict1 = false someDict2 = false someDict3 = true ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看