Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Dictionary in swift

Shulou Source: shulou.com Published: 2022-06-03 07:25:48 09月18日 Update

This article focuses on "how to use Dictionary in swift". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use Dictionary in swift.

Define / / define whether var dic1 = Dictionary () var dic2 = [String: Int] () var dic3: Dictionary = [:] var dic = ["zhang san": 18, "li si": 19, "wang wu": 20] is empty & number of elements print (dic.isEmpty) / / falseprint (dic.count) / / 3 traversal / / unordered traversal dictionary for (key, value) in dic {print ("name:\ (key)) Age:\ (value) ")} / * name: li si, age:19name: wang wu, age:20name: zhang san, age:18*/// ordered traversal for key in dic.keys.sorted () {print (" name:\ (key), age:\ (dic [key]!) ")} / * name: li si, age:19name: wang wu, age:20name: zhang san, age:18*/update//updatedic [" zhang san "] = 19print (dic) / / [" li si ": 19 "wang wu": 20, "zhang san": 19] / updateValuedic.updateValue (20, forKey: "zhang san") print (dic) / / ["li si": 19, "wang wu": 20, "zhang san": 20] remove//removedic ["zhang san"] = nilprint (dic) / / ["li si": 19 "wang wu": 20] / / removeValueprint (dic.removeValue (forKey: "li si")) / / Optional (19) print (dic) / / ["wang wu": 20] merge//mergevar mergeDic = ["a": 1, "b": 2] mergeDic.merge (["a": 3, "c": 4]) {(current, _) in current} print (mergeDic) / / [c ": 4," a ": 1 "b": 2] mergeDic.merge (["a": 3, "d": 6]) {(_, new) in new} print (mergeDic) / / ["b": 2, "c": 4, "a": 3, "d": 6] so far I believe you have a deeper understanding of "how to use Dictionary in swift", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Content learning practical deeper orderly element interest dictionary practicality practical simple operation quantity method more friends website channel query Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information Docker Shulou Tech Info Shulou Technology