1. SwiftUi and Xcode Basic Concepts

Swift review : Conditional operators and arrays 2.42/4.56
swift ใช้ struct แทน class for view/view controllers
struct เป็น value type , work different กับ var intn
– init เป็น implicit – unassign declared variable – implicity parameter เมื่อ callstruct
views – body UIViews – computed property returning – a view
– no computation code allowed
– one exception – if…else ^ถ้าไม่แสดง ก็น่าจะยุ่งเหมือนกัน เพราะใช้ condition eg user login ok หรือยัง
use conditional operators liberally
❈ Ternary conditional operator
let isPizza: Bool = false
let hello = isPizza ? “Huli Pizza” : “Hello World”
print(hello) // Hello World
❈ Nil Coalescing Operator
let imageName:String ? = nil
let presentedNmae = imageName ?? “Placeholder”
print(presentedName) // Placeholder
❈ Array

::: struct ,init เป็น implicit

ใช่ครับ ✅ ใน Swift โดยเฉพาะเวลาคุณใช้ structคำว่า init (initializer หรือ constructor) จะถูกสร้างขึ้นแบบ implicit (อัตโนมัติ) ถ้าไม่มีการกำหนดเอง ซึ่งเรียกว่า:

Memberwise Initializer


🔹 ตัวอย่าง struct + implicit init:

struct Person {
var name: String
var age: Int
}

คุณสามารถสร้าง Person ได้เลยแบบนี้:

let p = Person(name: "John", age: 30)

📌 เพราะ Swift สร้าง init(name:age:) ให้โดยอัตโนมัติ


❗ เมื่อไหร่จะไม่มี implicit init?

หากคุณ:

  1. กำหนด init เอง
  2. มี let properties ที่ไม่มีค่าเริ่มต้น
  3. มี private properties
  4. ใช้ @State, @ObservedObject, หรือ property wrappers บางอย่างใน SwiftUI

Swift จะ ไม่ สร้าง init ให้ หรืออาจต้องเขียนเอง


🔧 ตัวอย่างกำหนด init เอง:

struct Person {
var name: String
var age: Int

init(name: String) {
self.name = name
self.age = 0 // default age
}
}

ตอนนี้คุณจะใช้ได้แค่:

let p = Person(name: "Jane")  // OK
// Person(name: "Jane", age: 30) ❌ จะ Error

🧠 สรุป:

ลักษณะinit อัตโนมัติ (implicit)
struct มีแต่ var และค่ากำหนดครบ✅ มี
กำหนด init เอง❌ ไม่มี implicit แล้ว
let ไม่มีค่าเริ่มต้น❌ ต้อง init เอง
ใช้ property wrappers เช่น @State❌ ต้อง init เอง

ถ้าคุณใช้ struct ใน SwiftUI (เช่น View), การใช้ @State, @Binding, หรือการสื่อสารระหว่าง Views อาจต้องจัดการ init เอง

ใช้ conditional operator in body UIView

ใน SwiftUI (หรือ struct ที่ conform กับ View protocol), คุณสามารถใช้ conditional operator (หรือ ternary operator condition ? trueCase : falseCase) ภายใน body ได้โดยตรง เพื่อเลือกแสดง View ตามเงื่อนไขได้ครับ ✅


✅ ตัวอย่าง: ใช้ conditional operator ใน body

struct ContentView: View {
@State private var isLoggedIn = false

var body: some View {
VStack {
isLoggedIn ? Text("Welcome back!") : Text("Please log in")

Button(action: {
isLoggedIn.toggle()
}) {
Text("Toggle Login")
}
}
}
}

ผลลัพธ์:

  • จะแสดงข้อความ “Welcome back!” ถ้า isLoggedIn == true
  • จะแสดง “Please log in” ถ้า false
  • ปุ่มกดเปลี่ยนสถานะ

🔄 ถ้า View มีหลายบรรทัด (มากกว่า 1 View)?

ในกรณีที่คุณอยากแสดง View หลายรายการแบบ conditional — คุณ ควรใช้ if แทน ternary เพราะ ? : ใช้ได้แค่สำหรับ expression เดียว

swiftCopy codevar body: some View {
    VStack {
        if isLoggedIn {
            Text("Welcome")
            Image(systemName: "person.fill")
        } else {
            Text("Please Login")
            Image(systemName: "lock.fill")
        }
    }
}

🧠 สรุป:

รูปแบบใช้ได้กับ
condition ? view1 : view2✅ ใช้ได้เมื่อมีแค่ 1 View ในแต่ละด้าน
if ใน body✅ ใช้ได้และนิยมเมื่อมีหลาย View
switch ใน body✅ ใช้ได้ถ้าต้องเลือกหลายกรณี

หากคุณอยากใช้ conditional operator กับ ViewBuilder, หรือมีปัญหา compile เช่น Result builder does not support conditional operator — ผมสามารถช่วยแก้หรือ refactor ได้เช่นกันครับ.

Discover more from อรรถพรคลินิก ศัลยกรรมตกแต่ง Attaporn Plastic Surgery clinic 阿塔蓬博士,曼谷整形外科医生

Subscribe now to keep reading and get access to the full archive.

Continue reading