เรียนเขียน iOS App ด้วยภาษา Swift — ตอนที่ 3 : Collection Types (Arrays, Dictionaries and Sets)

ODENZA
3 min readDec 19, 2020

ในภาษา Swift นั้นจะมี collection types ด้วยกัน 3 แบบ ดังนี้

  • Arrays
  • Dictionaries
  • Sets

ทั้ง 3 แบบเป็นการเก็บข้อมูลหลายๆค่าโดยมี data type เดียวกัน (แต่จะมีวิธีการเข้าถึงข้อมูลที่แตกต่างกัน)

Arrays

Array เป็นการเก็บข้อมูลหลายๆค่าโดยมี data type เดียวกันและเก็บข้อมูลที่เป็นลำดับโดยใช้ index เป็นตัวอ้างอิง
การประกาศค่าเริ่มต้นของ Arrays (โดยส่วนตัวผมใช้แบบที่ 2 — 3 มากกว่า)

var array1 = Array<Int>()
var array2 = [String]()
var array3: [String] = []

ตำแหน่งของข้อมูลหรือ index ใน Array นั้น เริ่มจาก index ที่ 0
[index ที่ 0, index ที่ 1, index ที่ 2, …]

var colors: [String] = ["Red", "Green"]
var color0 = colors[0] //"Red"

การนับสมาชิกของ array

  • count นับสมาชิกว่ามีกี่ตัว ค่าที่ได้จะมี data type เป็น Int
colors.count   // 2

การตรวจสอบว่า array นั้นเป็นค่าว่างหรือไม่

  • isEmpty ค่าที่ได้จะมี data type เป็น Bool
    (ถ้า array นั้นว่างเปล่า จะได้ผลลัพธ์เป็น : true)
colors.isEmpty  //false

การเพิ่มข้อมูลใน array

  • append(new data here) เพิ่มข้อมูลต่อท้ายสุด
  • += เพิ่มข้อมูลต่อท้ายสุด
  • insert(new data here, at: ตำแหน่งของ index) เพิ่มข้อมูลโดยระบุตำแหน่ง
//เพิ่มข้อมูลต่อท้าย
// 1.
colors.append("Blue") //["Red", "Green", "Blue"]
// 2.
colors += ["Pink"] //["Red", "Green", "Blue", "Pink"]
//เพิ่มข้อมูลแบบเจาะจงตำแหน่ง โดย index
colors.insert("Yellow", at: 2)
//["Red", "Green", "Yellow", "Blue", "Pink"]

การลบข้อมูลใน array

  • remove(at: ตำแหน่งของ index) ลบตามตำแหน่งที่ระบุ index
  • removeFirst() ลบตัวแรก
  • removeLast() ลบตัวสุดท้าย
  • removeAll() ลบทั้งหมด
//ลบเฉพาะข้อมูลโดยเจาะจงที่ตำแหน่งของ index
colors.remove(at: 1) //["Red", "Yellow", "Blue", "Pink"]
//ลบข้อมูลตัวแรก
colors.removeFirst() //["Yellow", "Blue", "Pink"]
//ลบข้อมูลตัวหลังสุด
colors.removeLast() //["Yellow", "Blue"]
//ลบทั้งหมด
colors.removeAll() //[]

Dictionaries

เป็นการเก็บข้อมูลหลายๆค่าโดยมี data type เดียวกันเหมือนกับ array แต่ต่างกันที่ Dictionary เป็นเก็บข้อมูลแบบไม่เป็นลำดับ(index)

  • ใช้ unique key ในการเข้าถึงข้อมูล แทนการระบุลำดับ index

การประกาศค่าของ Dictionary

  • ประกาศแบบเต็ม Dictionary<key type, value type>
  • ประกาศแบบสั้น[key type : value type]
var user: Dictionary<String, String> = [:]
var user: Dictionary = [:]
var user: [String: String] = [:]
var user = [String: String]()
var user = [:]

การเพิ่มสมาชิกให้กับ Dictionary

  • dictionaryName[key name] = value
  • ใช้ method updateValue(value, forKey: key name)

Example :

var currency[String: String] = ["THB": "Thai Baht"]
currency["USD"] = "USD Dollar"
currency.updateValue("Japanese Yen", forKey: "JPY")
//Result :
//["THB": "Thai Baht", "JPY": "Japanese Yen", "USD": "USD Dollar"]

การลบสมาชิกใน Dictionary

  • dictionaryName[key name] = nil
  • ใช้ method removeValue(forKey: key name)

Example :

currency["THB"] = nil
or
currency.removeValue(forKey: "THB")
//Result : ["JPY": "Japanese Yen", "USD": "USD Dollar"]

สามารถใช้ countเพื่อนับจำนวนสมาชิกและใช้ isEmpty เพื่อตรวจสอบค่าว่างของ ได้เหมือนกับ array

Sets

เป็นการเก็บข้อมูลหลายๆค่าโดยมี data type เดียวกันเหมือนกับ array และ Dictionary แต่ set นั้น ไม่มีลำดับสมาชิกเหมือน array และไม่สามารถมีค่าที่ซ้ำกันได้

การประกาศ set Set<value type>

var currency = Set<String>()

การเพิ่มและลบสมาชิก

var currency = Set<String>()
currency = ["THB", "USD"]
or
var currency: Set = ["THB", "USD"]
//add value
currency.insert("JPY")
//remove value
currency.remove("USD")
//Result : ["THB", "JPY"]

การสร้าง Sets ใหม่

the swift programming language

จากรูปข้างบน

  1. intersection: คือ เอาค่าที่อยู่ใน a และ b ที่มีเหมือนกัน (ค่าซ้ำกัน)
  2. symmetricDifference: คือ ไม่เอาค่าที่ซ้ำ (ตรงข้ามกับ intersection)
  3. union: คือ เอาค่าทั้งหมดใน a และ b
  4. subtracting: คือ เอาค่าที่อยู่ใน a และค่านั้นต้องไม่อยู่ใน b

Examples:

let A: Set = [1, 3, 5, 7, 9]
let B: Set = [0, 2, 4, 6, 8]
let C: Set = [2, 3, 5, 7]
A.intersection(C).sorted()
//[3, 5, 7]
A.symmetricDifference(C).sorted()
//[1, 2, 9]
A.union(B).sorted()
//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
A.subtracting(C).sorted()
//[1, 9]

.sorted() คือการจัดเรียงค่าน้อยไปมาก

สรุป

  • Array เข้าถึงสมาชิกด้วย index และเป็นการเก็บข้อมูลที่เป็นลำดับ
  • Set ไม่มีลำดับสมาชิกเหมือน array
  • Dictionary เข้าถึงสมาชิกด้วย key และเป็นการเก็บข้อมูลที่ไม่เป็นลำดับ

เรียนเขียน iOS App ด้วยภาษา Swift แบบพื้นฐาน เรียนด้วยตัวเอง

ตอนที่ 1 : การประกาศตัวแปร (variable & constant)
https://bit.ly/3nFVBCc

ตอนที่ 2 : ชนิดของตัวแปร (Data Types)
https://bit.ly/3h2N4Xb

ตอนที่ 3 : Collection Types (Arrays, Dictionaries and Sets)
https://bit.ly/3pkJfzR

ตอนที่ 4: Operators ( การใช้เครื่องหมายทางคณิตศาสตร์)
https://bit.ly/2WrTcyW

ตอนที่ 5 : การทำงานแบบมีเงื่อนไข (Conditional statements)
https://bit.ly/3pe8lQR

ตอนที่ 6 : การวนซ้ำ Loops (for-in, while)
https://bit.ly/3h1aNae

ตอนที่ 7 : Functions (ฟังก์ชัน)
https://bit.ly/3rbAc5O

--

--