"Swift from entry to proficiency" ~ basic articles (collections, arrays, dictionaries)

Contact: Shihu QQ: 1224614774    Nickname:  Om Mani Padme Coax

                      QQ group: 807236138   Group name:  iOS technology exchange learning group

                      QQ group: 713799633   Group name: iOS technology exchange learning group-2

reference:

"Swift from entry to proficiency" ~ basic articles (data types) https://blog.csdn.net/shihuboke/article/details/109553998
"swift from entry to proficiency" ~ basic articles (operators, strings) https: //blog.csdn.net/shihuboke/article/details/110203341

"Swift from entry to proficiency" ~ basic articles (control flow, function) https://blog.csdn.net/shihuboke/article/details/112144958

1. Collection concept

集合就是用来存储一组数据的容器,常用的集合类型: 数组、集合和字典

数组是种按顺序存储相同类型数据的集合,相同的值可以在数组中的不同位置重复出现

集合和字典也是存储相同类型数据的集合,但是数据之间是无序,集合不允许重复出现

字典可以重复出现,但是每一个值都有唯一的健值与其对应

1. Array

数组的声明格式: Array<DataType> 或者[DataType]

There are three types of array initialization

The first:

初始化方法是先将数组声明为一个空数组

var animalArray = [String]() // 初始化一个animalArray的空数组

animalArray.addend(“qqq”)

The second type:

初始化的方法是声明数组的时候直接复制为一个实际的数组。

var  oneBitNumberArray: Array<Int> = [0,1,2,3,4,5,6]

var botanyArray1: [String] = [“1213”,”2345”,””456]

var botanyArray2:  [“1213”,”2345”,””456]

The third type:

初始化的方法是快捷初始化方法,即一次性将数组的所有的元素初始化为同一个值

var twoBitNumberArray: [Int](repeating: 0, cout: 3) // 打印结果 0,0,0,

var threeBitNumberArray: [Int](repeating: 11, cout: 4) // 打印结果 11,11,11,11,

Array addition and accumulation operations

数组的相加和累加运算分别使用运算符“+”和“+=”,从而由已知数组得到新数组

var theAddedBNumberArray =  twoBitNumberArray + threeBitNumberArray // 打印结果【0,0,0, 11,11,11,11,】

animalArray += [“www”]  // 打印结果  [“qqq”,“www”]

animalArray += [“rrrr”,”eeee”] // 打印结果 [“qqq”,“www”,“rrrr”,”eeee”]

Array subscript operation

数组中的元素是有序的,通过下标可以找到特定位置的元素

var theFirstAnimal = animalArray[0 ]  // 打印结果  “qqq”

animalArray[2…3]  // 打印结果  “rrrr”,”eeee”


注意: [2…3]的角标是是从 0 开始的,2..3 是第二个元素到第三个元素(包括 2 和 3)

Insert and delete elements

insert()

insert(at: Int)

remove()

removeFirst()

removeLast()


theFirstAnimal.insert(“ttt”, at: 3)  // 打印结果  [“qqq”,“www”,“rrrr”,”eeee”,“ttt”] 是从 0 小标开始的

array traversal

对 for- in 循环对数组animalArray中的元素进行遍历打印

For  animal in animalArray {

//打印的是 四个 item

}


对以元组的方式对数组中的下标和相应元素遍历并打印

for (index,animal ) in {

animalArray() {

print()

  }

}

slice of the array

通过数组的下标操作可以获取数组汇总的某一个元素,如果需要获取数组的一部分或者多个元素,就要用到获取子数组的方法

获取数组 Array 的片段 arraySlice的格式为:

arraySlice = array[startIndex…endIndex]


补充: 数组片段是原数组的一部分,共用相同的存储空间。如果数组片段中元素的值发生变化,那原数组中相应的元素的值也会同步改变

let newZoo = Array(animalArray[1…2])

newZoo[0]

newZoo[1]

element exchange position

使用方法 swapAt(_:_:)

animalArray.swapAt(2, 3)

array sort

使用方法 sort()

retrieve a specific element

使用方法 contains(_:) 该方法根据检索结果返回布尔值

2. Collection

A set is composed of a group of elements of the same data type, each element has a unique value, and the elements in the set are unordered.

集合类型的声明格式为 Set<DataType>

Collection initialization

Create an empty collection to start, or through direct assignment

The first type: need to display the element type,

var weatherOfSanya = Set<String>()

weatherOfSanya = [“晴天”,“阴天”,“大雨”]

The second: infer the element type in the collection from the assigned initial value

weatherOfSanya :Set =  [“晴天”,“阴天”,“大雨”]

Set empty judgment and insert elements

——同数组方法

delete element

——同数组方法

retrieve a specific element

——同数组方法

iterate through the collection

——同数组方法

collection sort

使用方法 sorted()函数

Operations between sets

intersect() 方法计算二个集合的交集,结果为一个新的集合

union()     方法计算二个集合的并集,结果为一个新的集合

subtract()  方法计算一个集合变量中不属于另一个集合的部分,结果为该部分元素形成的新集合

2. Dictionary

声明字典类型的格式为:Dictionary<KeyType,ValueType>

声明简化格式: [KeyType :ValueType]

dictionary initialization

创建一个空字典

var ascIIDictChar = Dictionary<Int , Character>()

var ascIIDictChar = [Int : Int]()

Two ways to update dictionary elements

第一种是利用下标语法

第二种是利用字典类型自带的方法和属性

var ascIIDictChar = [97:”a”,98 : “b”,99 : “c”,100 : “d”,101 : “e”,102 : “f”]

利用下标创建一个县的键值对,键为 103,值为“g”

ascIIDictChar[103] = “g”

修改已经存在的键值对[97:”a”]为[97:”A”]

ascIIDictChar[97] = “A”

Two ways to delete dictionary elements

第一种:  ascIIDictChar[97] = nil // 打印结果 [98 : “b”,99 : “c”,100 : “d”,101 : “e”,102 : “f”]

第二种:  if let removeValue = ascIIDictChar.removeValue(forKey:98) {

  // 打印结果 [99 : “c”,100 : “d”,101 : “e”,102 : “f”]

}

iterate over the dictionary

可以用 for-in 中循环中逐一访问键值对,并用元组保存,是用元组变量(ascIICode , char)储存每一次循环并打印出键值对

//字典的 keys 属性和 values 属性

  字典类型提供了 keys 和 values 属性分别表示所有键和所有值得集合


 例子:
   for ascIICode  in  ascIIDictChar.keys {}

   for char  in  ascIIDictChar.values {}


 为了便于使用,一般将其转换为一个数组变量

   let  ascIICodeArray = Array(ascIIDictChar.keys)

   let charArray = Array(ascIIDictChar.values )

2. Control flow

           Please see the next text...

         "Swift from entry to proficiency" ~ basic articles (control flow, function) https://blog.csdn.net/shihuboke/article/details/112144958

Thanks!!!

This account mainly shares my various experiences and insights in the process of growing up, including technical summaries, iOS, Swift and Mac related technical articles, tool resources, participation in technical discussions, sorting out development skills, and making learning a pleasure!

Guess you like

Origin blog.csdn.net/shihuboke/article/details/110672581