Swift 字典的常用方法


        var dict1 = [1:"A",2:"B",3:"C",4:"D",5:"E"];
        var test1 = ["key1":"你好","key2":"Swift","key3":"正在學習","key4":"字典","key5":"取值"]
        //根據鍵去取值
        print(test1["key1"]);
        //字典元素的個數
        print(test1.count);
        //添加字典元素
        dict1[6] = "F";
        test1["key"] = "test";
        //刪除字典中的元素
        test1.removeValue(forKey: "key1");
        //修改字典元素
        test1["key"] = "李平";
        //遍歷
        for (key,value) in test1{
            print("key\(key),value,\(value)");
        }
        for testKey in test1.keys{
            print(testKey);
        }
        for testValue in test1.values{
            print(testValue);
        }
        //把字典里的鍵或值轉化為數組
        let textKeyArr = Array(test1.keys);
        print(textKeyArr);
        let textValueArr = Array(test1.values);
        print(textValueArr);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM