Swift = ภาษาเขียนแอป/เกม iPhone, iPad, Mac, Apple Watch + — Apple ออกแบบให้ ประสิทธิภาพสูงสุด — Swift 5 ขยายฟีเจอร์เดิมที่น่าประทับใจ — บทความนี้: ใช้ Swift 5 ยังไง, ทำไมควรใช้, ฟีเจอร์ใหม่ทั้งหมด
อยากลงมือทันที: เริ่มต้น Swift 5 — ภาพกว้าง: Complete Guide to Mac Programming (ภาษาอื่นบน macOS)
ภาพรวม Swift 5
Swift 5 = ภาษาพัฒนา Apple → สร้างแอป iOS, macOS, tvOS, watchOS
v5 วางจำหน่ายคู่ Xcode 10.2 (มี.ค. 2019) — Swift 1.0 = ก.ย. 2014 — Apple: Swift เร็วกว่า Objective-C 2.6x และ Python 8.4x
ทำไมควรเขียน Swift 5?
- Open Source: โค้ดเปิดสาธารณะ — ใครก็ inspect/modify/deploy ได้ — Apple: Open Source ทำ macOS แข็งแกร่ง/ปลอดภัย (Peer Review ทศวรรษ)
- เรียนรู้ง่าย: Syntax เรียบง่าย — เริ่มต้นเยี่ยม — มีประสบการณ์ Dev? Syntax/Concepts ใกล้เคียงภาษาเดิม
- เร็ว: อัลกอริทึมค้นหา เร็วกว่า Objective-C 2.6x, Python 8.4x
- ปลอดภัย: ไม่มี Unsafe Code, ใช้ Convention สมัยใหม่รักษาความปลอดภัย
- Playgrounds: Xcode มี Playgrounds เขียนโค้ด → เห็นผลทันที — ใช้ Swift Playgrounds
- Future-proof: พัฒนา iOS, macOS, watchOS, tvOS ได้
- พัฒนาต่อเนื่อง: Swift 5 = ABI Stability (รอคอยนาน) — Compiler ใหม่ compile โค้ด Swift 5+ ได้, Migration รุ่นใหม่ง่ายกว่า — OS Vendor 埋め込み Swift Standard Library ได้ เข้ากันได้กับแอป Swift 5+
WWDC 2019 คาดว่าจะมีข่าว Swift เพิ่ม
เริ่มต้น Swift 5
พัฒนา iOS ต้อง: Mac (MacBook, iMac, Mac mini) + Xcode ฟรี (v10.2+)
- Mac App Store → ค้น "Xcode"
- Get ถัด Xcode
- หา Xcode บน Mac App Store ผ่าน Browser ได้

Online Compilers (ไม่ Deploy)
- Online Swift Playground: รองรับ Swift 5, รันทดสอบโค้ด
- Repl.it: รองรับ Swift 4.2 (พ.ค. 2019) อัปเดตเร็ว
เขียนแอปง่ายๆ ด้วย Swift
Xcode → File > New > Project → Single View App

ชื่อแอป + Organization Name (บริษัท/ตัวเอง) — Organization Identifier = URL บริษัทย้อนกลับ (เช่น com.mycompany.myapp) — ภาษา: Swift → Next → เลือกที่เซฟ

ได้หน้าจอ:

สร้างแอปง่าย: User พิมพ์ชื่อใน Text Field → กดปุ่ม → Label โชว์ Greeting
ไป Main.storyboard (ซ้าย) → View ว่าง → ปุ่มกลมมุมขวาบน (Object Library) → ลาก Text Field, Label, Button ลง View


เลือก Button → Utility ขวา → Title: Generate greeting

ดับเบิลคลิก ViewController.swift → เปิดหน้าต่างแยก
เลือก Text Field → Ctrl+Drag → บนสุด Class ViewController → สร้าง IBOutlet ชื่อ textField

Label เดียวกัน → IBOutlet ชื่อ label — Button: Ctrl+Drag → ล่าง Class → สร้าง IBAction ชื่อ buttonTapped
IBOutlet = เข้าถึง UI ในโค้ด, IBAction = ตอบสนอง Event (กดปุ่ม)
โค้ดใน buttonTapped:
if let name = textField.text {
self.label.text = "Hello " + name
}
ViewController.swift ควรเป็น:

กด Run → รันบน Simulator
User พิมพ์ชื่อ → กดปุ่ม → Label โชว์ "Hello [ชื่อ]"

แนวคิด Swift 5 (Basic & Advanced)
ทำแอปง่ายแล้ว → ไป Methods/Code Snippets สำหรับโปรเจกต์จริง
print("Hello, world!")
Variables & Constants
let = Constant (เปลี่ยนไม่ได้), var = Variable (เปลี่ยนได้) — Type Inference: ใส่ค่า = Compiler รู้ Type
let constVar = 42
var numberVar = 27
ระบุ Type เองได้:
var numberVar: Int = 27
Comments
Single-line: // comment — Multi-line: /* multi\nline */
Decision Making (if/else)
if boolean_expression {
// true
}
if boolean_expression {
// true
} else {
// false
}
if boolean_expression_1 {
// true 1
} else if boolean_expression_2 {
// true 2
} else if boolean_expression_3 {
// true 3
} else {
// none true
}
ตัวอย่างรูป:

Switch Statement
switch expression {
case expression1:
statement(s)
fallthrough // optional
case expression2, expression3:
statement(s)
fallthrough // optional
default: // optional
statement(s)
}
ตัวอย่าง: 
Arrays
var arrayList = ["Swift", "JavaScript", "Java", "PHP"]
arrayList[2] = "React Native" // แก้ index 2
var emptyArray = [String]() // หรือ []
Dictionaries
var occupations = ["Steve": "Developer", "Kate": "Designer"]
occupations["Steve"] = "CTO"
occupations = [:] // empty
Sets
var a: Set = [1,2,3,4,5,6,7,8,9,0] // ค่า unique เท่านั้น
Optionals: จัดการ "ไม่มีค่า" — var myString: String? — ? = มี/ไม่มีได้, ! = เริ่ม nil แต่ต้องมีค่าตอนใช้ (Runtime Error ถ้าไม่มี), ไม่มีเครื่องหมาย = ต้องมีค่า (Compile Error ถ้าไม่มี)
Functions
func createStr(Number inputNum: Int) -> String {
return "\(inputNum)"
}
createStr(Number: 345) // เรียกใช้
Classes
class Car {
var numOfPersons: Int?
func displayDetails() { }
}
var myCar = Car()
myCar.numOfPersons = 5
Closures
{ (parameters) -> return type in statements }
ตัวอย่าง: กำหนด Closure ให้ตัวแปร scname → เรียกผ่านชื่อตัวแปร — 
Extensions
ขยาย Class/Struct/Enum ที่มีอยู่ — เพิ่มฟังก์ชันได้ ไม่ Override — ตัวอย่าง: Class Car + Extension เพิ่ม Property Speed → เรียก Speed โดยตรง — 
Tuples
var TupleName = (Value1, value2, ...)
var error501 = (501, "Not implemented")
ฟีเจอร์ใหม่ Swift 5
Raw Strings
สร้าง String ที่มี Quote/Backslash ไม่ต้อง Escape — ใส่ # หน้า/หลัง String — Quote/Backslash = ตัวอักษรธรรมดา

String Interpolation ใน Raw String: ใส่ # เพิ่มหลัง Backslash

ต้องใช้ "# ใน Raw String: ใส่ ## หน้า/หลัง

ตรวจสอบ Integer Multiples (isMultiple)
Swift 4.2: ใช้ Modulo % — Swift 5: มี Method เฉพาะ isMultiple(of:) อ่านง่ายกว่า

จัดการ Enum Cases อนาคต (@unknown default)
Switch ต้อง Exhaustive (ครอบทุก Case) — ปกติ: Default จับ Case อื่นทั้งหมด — ปัญหา: เพิ่ม Case ใหม่ภายหลัง → Compiler ไม่เตือน → Default จับไป → อาจไม่ใช่ที่ต้องการ
Swift 5: @unknown attribute กับ default
→ 
@unknown default = Compiler จะ Warning ถ้า Enum เพิ่ม Case ใหม่ → Dev ตัดสินใจจัดการต่อ
Flatten Nested Optionals (try?)
Nested Optional จาก try? — Swift 5: Flatten เป็น Regular Optional — เหมือน Conditional Cast/Optional Chaining

ตัวแปร model เป็น String? ไม่ใช่ String?? (Swift 4.2)
Result Type ใน Standard Library
Swift 5 เพิ่ม Result Type ใน Stdlib — จัดการ Error Async Code สะอาด — Enum: success, failure (Generics) — Success: Associated Value อะไรก็ได้, Failure: ต้อง Conform Error Protocol
ตัวอย่าง: 
ApiClient fetch names from URL — Completion Closure รับ Result<[String], ApiError>
ใช้งาน: 
ฟีเจอร์ Swift 4 ที่ยังคงใช้
Strings (Collection Protocol)
Swift 4: String conforms Collection → Iterate ตรงได้ + ใช้ Method Collection: count, isEmpty, map, filter, index(of:) ฯลฯ

Multi-line String: """ ... """ (Triple Quotes) — ไม่ต้อง Escape Double Quotes

JSON Encoding/Decoding (Codable)
Swift 4: 簡單化 JSON Archive/Serialise — Custom Type implement Codable (Encodable + Decodable)

Key Paths ฉลาดขึ้น
เข้าถึง Property ด้วย Key Paths ง่ายขึ้น

ผสม Class + Protocol
Swift 3: รวม Protocol ตอนสร้าง Constant/Variable — Swift 4: + Class ด้วย Syntax เดียวกัน — Constrain Object ให้เป็น Class + Protocol พร้อมกัน (เหมือน Objective-C)
swap vs swapAt
Swift 3: swap(_:_:) Mutating สลับ 2 Element — ส่ง Element ตรงเข้า Function — Swift 4: swapAt(_:_:) สลับด้วย Index แทน
Dictionaries & Sets
Dictionary init(uniqueKeysWithValues:) สร้าง Dictionary ใหม่จาก Array of Tuples

แหล่งเรียน Swift 5 ที่ดีที่สุด
- Apple Documentation: แหล่งทางการดีที่สุด
- eBook: Apple เปิด The Swift Programming Language (Swift 5.0) ใช้เรียนดีมาก
- Udemy: คอร์ส Swift 5: Swift 5 Programming for Beginners, iOS 12 & Swift 5: Build a To Do List App
- Swift Programming in Easy Steps (หนังสือผู้เขียนบทความนี้) — สอนสร้าง iOS App Swift 5 จากศูนย์ + รูปประกอบ — Amazon
- Hacking with Swift (Paul Hudson) — เว็บ/หนังสือ สำหรับ Dev Swift — เว็บไซต์
แหล่งเพิ่ม: How to learn Swift