รู้จักกับ Collection
ใน swift นั้นจะมี collectionให้ใช้กัน 3 ประเภทหลักๆ คือ
- Array ซึ่งเป็น collection ที่เก็บข้อมูลตามลำดับ
- Set เก็บข้อมูลไม่เป็นลำดับ
- Dictionary เก็บข้อมูลไม่เป็นลำดับ
เวลาทำการสร้าง collection แบบ Array, Set หรือ Dictionary ถ้าเราทำการสร้าง collection เหล่านั้นด้วย “var”(variable) collection นั้นจะถูกสร้างเป็นแบบ mutable คือสร้างมาปรับเปลี่ยนข้อมูลด้านในได้ แต่ถ้าเวลาสร้าง collection เหล่านั้นด้วย “let”(constant) แล้วละก็ collection ก็จะเป็นแบบ immutable คือไม่สามารถเปลี่ยนแปลงแก้ไขข้อมูลได้อีกแล้ว
- Array จะสามารถเก็บค่าที่เป็นชนิดเดียวกันลงไป และเก็บข้อมูลแบบเรียงลำดับ ข้อมูลไหนมาก่อนใส่ก่อน ข้อมูลไหนมาหลังจะใส่ทีหลัง และ ค่าเดียวกันก็สามารถถูกบรรจุลงไปหลายครั้งใน collection เดียวกันได้การสร้าง Array ใน swift อาจใช้ Array<Element> หรือเขียนในรูปย่อได้คือ [Element] แม้ว่าทั้งสองแบบจะให้ผลเหมือนกัน แต่การเขียนแบบย่อจะเป็นที่นิยมมากกว่าตัวอย่างการสร้าง Array
var myMealToday = Array<String>() var myMealTomorrow = [String]()
เนื่องจาก swift นั้นมี type inference เพราะฉะนั้น ตอนประกาศตัวแปร var mealToday เราอาจยังไม่ต้องประกาศประเภทของข้อมูลก็ได้ เพราะ หลักจากกำหนดค่าให้ตัวแปรนั้นแล้ว ชนิดของตัวแปรจะถูกกำหนดเองตามประเภทของค่าที่กำหนดนั้น
ตัวอย่างการสร้าง Array และค่าตั้งต้น
var scores = [Int](count:50, repeatedValue:0)
จากตัวอย่างข้างต้นคือ การสร้าง Array โดยมีค่าตั้งต้น จากตัวอย่างกำหนดให้มีขนาด(count) 50 และค่า default(repeatedValue) ทุกตัวคือ 0
ตัวอย่างการสร้าง Array แบบกำหนดค่า
var studentRoomA : [String] = ["David", "Peter", "John"] // กำหนด type ของ Array var studentRoomB = ["Frank", "James", "Anna"] // ไม่กำหนด type ของ Array ใช้swift's type inference กำหนดให้เป็น Array ของ String
ตัวอย่างฟังก์ชันใน Array
var studentRoomA : [String] = ["David", "Peter", "John"] var studentRoomB = ["Frank", "James", "Anna"] print("Room A has students:\(studentRoomA.count)") // Result: 3 // New student attend room A studentRoomA.append("Nick"); print(studentRoomA) // Result: ["David", "Peter", "John", "Nick"] // First student left room A studentRoomA.removeAtIndex(0) print(studentRoomA) // Result: ["Peter", "John", "Nick"]ตัวอย่างการวนดูข้อมูลใน Array
var studentRoomA : [String] = ["David", "Peter", "John"] for student in studentRoomA{ print(student) } - Set ใช้เก็บค่าของข้อมูลประเภทเดียวกัน แบบไม่เรียงตามลำดับ และแต่ละข้อมูลต้องมีความแตกต่างกัน เราจะใช้ Set แทน Array ก็ต่อเมื่อลำดับของข้อมูลไม่มีความสำคัญ หรืออาจใช้ก็ต่อเมื่อต้องการมั่นใจว่าข้อมูลนั้นๆ มีแค่ข้อมูลเดียวใน collection
การสร้าง Set เขียนได้ดังนี้ Set<Element>
ตัวอย่างการสร้างเซตเปล่าvar fruitInAnnaBasket = Set<String>()
ตัวอย่างการสร้างเซตจาก array
var fruitInMyBasket:Set<String> = ["banana", "mango", "apple"] // แบบที่ 1 var fruitInPeterBasket:Set = ["banana", "mango", "apple"] // แบบที่ 2
จากแบบที่ 1 เรากำหนดประเภทของ Set ไปให้ตอนประกาศตัวแปร Set<String> ในแบบที่ 2 เราไม่ต้องกำหนดประเภทของตัวแปรลงไปก็ได้ เนื่องจาก swift’s type inference แต่ที่เราต้องเขียน Set กำกับลงไปเพราะหากไม่ใส่ swift เองจะสับสนระหว่างการประกาศ Set และ Array
ฟังก์ชันที่ใช้กับ Set ได้
var fruitInAnnaBasket = Set<String>() fruitInAnnaBasket.insert("pomelo") fruitInAnnaBasket.insert("grape") print(fruitInAnnaBasket) // Result: ["pomelo", "grape"] if !fruitInAnnaBasket.contains("orange") { fruitInAnnaBasket.insert("orange") } print(fruitInAnnaBasket) // Result: ["pomelo", "grape", "orange"]ตัวอย่างการวนดูข้อมูลใน Set
var fruitInMyBasket:Set<String> = ["banana", "mango", "apple"] for fruit in fruitInMyBasket { print("I can eat \(fruit)") } - Dictionary ใช้เก็บข้อมูล key, value ของข้อมูลประเภทเดียวกันไว้โดยไม่มีการเรียงลำดับข้อมูล เราจะใช้ Dictionary ก็ต้องเมื่อเราต้องทำการค้นหาข้อมูลจาก key ที่กำหนดไว้ หลักการนั้นเป็นหลักการเดียวกันกับการค้นหาคำศัพท์จากดิกชันนารีภาษาอังกฤษเพื่อค้นหาความหมายที่เราต้องการ วิธีการสร้าง Dicitionary<key, value> หรือแบบย่อก็คือ [key:value] ทั้งสองแบบจะให้ผลเหมือนกัน แต่แบบย่อจะเป็นที่นิยมมากกว่าตัวอย่างการสร้าง Dictionary
var contacts1 = [String:String]() var contacts2 = Dictionary<String, String>()
ตัวอย่างการสร้าง Dictionary พร้อมกำหนดค่า
var contacts:[String:String] = ["David": "David@booboohome.com", "Peter": "Peter@booboohome.com","John": "John@booboohome.com"]
ตัวอย่างฟังก์ชันที่ใช้ได้
var contacts:[String:String] = ["David": "David@booboohome.com", "Peter": "Peter@booboohome.com","John": "John@booboohome.com"] print("Amount: \(contacts.count)") contacts["David"] = "David.Bay@booboohome.com" // change email for David contacts.updateValue("Peter.West@booboohome.com", forKey: "Peter") // change email for Peter contacts["John"] = nil // remove John from Dictionary contacts.removeValueForKey("Peter") // remove Peter from Dictionary print(contacts) // Result: ["David": "David.Bay@booboohome.com"]ตัวอย่างการวนดูข้อมูลใน Dictionary
for(name, email) in contacts{ print("Name:\(name) , Email:\(email)") }ผลก็คือ
Name:David , Email:David.Bay@booboohome.com
วันต่อไป -> Function
