This is the fourth day of my participation in Gwen Challenge

Swift provides three main collection types, called arrays, collections, and dictionaries, for storing collections of values.

An array is an ordered set of values and a set is an unordered set of unique values and a dictionary is an unordered set of key-value associations

An array of

Array initialization

// Create an empty array
let arr = [Int]()
// Create an array with default values, all the same
let arr1 = Array(repeating: 2.count: 3)  / / (2, 2, 2]
// Construct arrays with array literals
let arr2: [String] = ["a"."b"]
var arr3 = ["a"."b"]
Copy the code

Whether the array is empty

if arr.isEmpty {
    print("Empty.")}Copy the code

Number of array elements

let count = arr.count;
Copy the code

Array index

let arr = ["a"."b"."c"."d"]
var value = arr[2]  //c
Copy the code

Array adds a new element

  • Tail add (append)
var arr = [1.2.3.4]
arr.append(5)
print(arr)  //[1, 2, 3, 4, 5]
Copy the code
  • Specify index add
var arr: [String] = ["a"."b"."c"."d"]
arr.insert("W".at: 2)
print(arr)  //["a", "b", "W", "c", "d"]
Copy the code
  • Two arrays
var arr = ["a"."b"."c"]
var arr1 = ["A"."B"."C"]
let allArr = arr + arr1
print(allArr)  //["a", "b", "c", "A", "B", "C"]
Copy the code

Array element modification

  • Modify a single element in an array
var arr: [String] = ["a"."b"."c"."d"]
arr[2] = "W"
print(arr)  //["a", "b", "W", "d"]
Copy the code
  • Modify a range of elements in an array
var arr: [String] = ["a"."b"."c"."d"."e"."f"."g"]
arr[2... 5] = ["X"."Y"."Z"]
print(arr)  //["a", "b", "X", "Y", "Z", "g"]
Copy the code

Array element deletion

var arr: [String] = ["a"."b"."c"."d"."e"."f"."g"]
arr.remove(at: 2)
print(arr)  // ["a", "b", "d", "e", "f", "g"]

arr.removeLast()
print(arr)  // ["a", "b", "d", "e", "f"]

arr.removeAll()  / / []
Copy the code

Array traversal

var arr: [String] = ["a"."b"."c"]
for item in arr {
    print(item)
}
//a
//b
//c


for (index ,value) in arr.enumerated() {
    print("\(index)---\(value)")}//0---a
//1---b
//2---c
Copy the code

A collection of

Set initialization

// Create an empty collection
var set = Set<String> ()// Construct with array literals
var set :Set<String> = ["a"."b"."c"]
var set :Set = ["a"."b"."c"]
Copy the code

Whether the array is empty

if set.isEmpty {
    print("Empty.")}Copy the code

Number of set elements

let count = set.count;
Copy the code

Set the index

let set = ["a"."b"."c"."d"]
var value = set[2]  //c
Copy the code

Add new elements to the collection

var set :Set<String> = ["a"."b"."c"]
set.insert("W")
print(set)  //["b", "c", "a", "W"]
Copy the code

Collection element deletion

var set :Set<String> = ["a"."b"."c"]
set.remove("b")
print(set)  //["c", "a"]

set.removeAll() / / []
Copy the code

Set other operations

let setA :Set<String> = ["a"."b"."c"]
let setB :Set<String> = ["a"."b"."c"."d"."e"]
Copy the code
  • Intersection (intersection)
print(setA.intersection(setB))  //["c", "a", "b"]
Copy the code
  • And set (union)
print(setA.union(setB))  //["b", "e", "d", "a", "c"]
Copy the code
  • Remove intersection (symmetricDifference)
print(setA.symmetricDifference(setB))  //["d", "e"]
Copy the code
  • If a value is not in the specified set, a new collection is created
print(setB.subtracting(setA))  //["d", "e"]
Copy the code
  • Checks whether all values of a collection are contained in the specified collection
print(setA.isSubset(of: setB))   //true
Copy the code
  • Determines whether a collection contains all the values in the specified collection
print(setA.isSuperset(of: setB))  //false
Copy the code
  • Determines whether a set is a subset or superset, but not equal to the specified set
print(setA.isStrictSubset(of: setB))  //true
Copy the code
  • Determines whether two sets have no value in common
print(setA.isStrictSubset(of: setB))  //false
Copy the code

A collection of traverse

for value in set {
    print(value)
}
//a
//c
//b

// The set is sorted first
for value in set.sorted() {
    print("\(value)")}//a
//c
//b
Copy the code

The dictionary

Dictionary initialization

// Construct the syntax
var dic = [Int: String] ()// Create a dictionary with default values
var dic : [String:String] = ["key":"value"]
var dic = ["key":"value"]
Copy the code

Whether the dictionary is empty

if dic.isEmpty {
    print("Empty.")}Copy the code

Number of dictionary key-value pairs

let count = dic.count;
Copy the code

Dictionary adds key-value pairs

var dic = ["keyA":"a"."keyB":"b"]
dic["keyC"] = "c"
//["keyC": "c", "keyA": "a", "keyB": "b"]
Copy the code

Dictionary modification element

  • Use subscript syntax to change the value corresponding to a particular key
var dic = ["keyA":"a"."keyB":"b"]
dic["keyA"] = "W"
print(dic) //["keyB": "b", "keyA": "W"]
Copy the code
  • If there is a corresponding key, the corresponding value is directly updated and the old value is returned
var dic = ["keyA":"a"."keyB":"b"]
let oldValue = dic.updateValue("W".forKey: "keyA")

print(dic)  //["keyA": "W", "keyB": "b"]
print(oldValue)  //Optional("a")
Copy the code
  • If there’s no corresponding key, it just adds the element and returns nil
var dic = ["keyA":"a"."keyB":"b"]
let oldValue = dic.updateValue("W".forKey: "keyC")

print(dic)  //["keyC": "W", "keyA": "a", "keyB": "b"]
print(oldValue)  //nil
Copy the code

The dictionary to delete

var dic = ["keyA":"a"."keyB":"b"]
dic["keyA"] = nil
print(dic)  //["keyB": "b"]

dic.removeValue(forKey: "keyB")
print(dic)  / / / :
Copy the code

The dictionary traversal

var dic = ["keyA":"a"."keyB":"b"]

for (key,value) in dic {
    print("\(key)---\(value)")}//keyB---b
//keyA---a


for key in dic.keys {
    print("\(key)")}//keyB
//keyA


for value in dic.values {
    print("\(value)")}//b
//a
Copy the code

Assignment and copy behavior for string, array, and dictionary types

In Swift, many of the basic types, such as String, Array, and Dictionary, are implemented as structures. This means that when assigned to a new constant or variable, or when passed into a function or method, their value is copied.

The NSString, NSArray, and NSDictionary types in OC are implemented as classes, not constructs. They do not copy values when assigned or passed to functions or methods, but pass references to existing instances.