ရိုးရိုးလေး ရှင်းပြမယ်
Data Types ကို ရိုးရိုးလေးနားလည်မယ်
Swift က type-safe language ဖြစ်ပါတယ်။ ဆိုလိုတာက variable တစ်ခုမှာ ဘယ် data type သိမ်းမလဲဆိုတာကို compiler က တိတိကျကျသိထားရပါတယ်။ ဒါကြောင့် number ထဲကို text ထည့်တာ၊ text ထဲကို boolean ထည့်တာလို mistake တွေကို စောစောဖမ်းပေးနိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
let age = 24 ပြီးနောက် age = "twenty four" လို့ assign လုပ်လို့မရပါ။ Type မတူလို့ error ဖြစ်ပါမယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Data Types ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
- Typed value
- Swift expression / function
- Safe result
Data Types ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
let name: String = "Nandar" // Explicit type
let age = 24 // Int လို့ compiler ကခန့်မှန်းမယ်
let rating = 4.8 // Double
let isPremiumUser = true // Bool
print("\(name) is \(age) years old.")
print("Rating: \(rating), Premium: \(isPremiumUser)")Data Types ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Typed value
let name: String = "Nandar" // Explicit typeSwift က type-safe language ဖြစ်ပါတယ်။ ဆိုလိုတာက variable တစ်ခုမှာ ဘယ် data type သိမ်းမလဲဆိုတာကို compiler က တိတိကျကျသိထားရပါတယ်။ ဒါကြောင့် number ထဲကို text ထည့်တာ၊ text ထဲကို boolean ထည့်တာလို mistake တွေကို စောစောဖမ်းပေးနိုင်ပါတယ်။
- Swift expression / function
let isPremiumUser = true // BoolData Types ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
- Safe result
print("Rating: \(rating), Premium: \(isPremiumUser)")Data Types example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Typed value
- Swift expression / function
- Safe result
Success
Data Types ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
let age = 24 ပြီးနောက် age = "twenty four" လို့ assign လုပ်လို့မရပါ။ Type မတူလို့ error ဖြစ်ပါမယ်။
Swift က type-safe language ဖြစ်ပါတယ်။ ဆိုလိုတာက variable တစ်ခုမှာ ဘယ် data type သိမ်းမလဲဆိုတာကို compiler က တိတိကျကျသိထားရပါတယ်။ ဒါကြောင့် number ထဲကို text ထည့်တာ၊ text ထဲကို boolean ထည့်တာလို mistake တွေကို စောစောဖမ်းပေးနိုင်ပါတယ်။
Swift မှာ type ကို ကိုယ်တိုင်ရေးနိုင်သလို compiler က တန်ဖိုးကြည့်ပြီး ခန့်မှန်းပေးတဲ့ type inference လည်းရှိပါတယ်။
let name: String = "Nandar" // Explicit type
let age = 24 // Int လို့ compiler ကခန့်မှန်းမယ်
let rating = 4.8 // Double
let isPremiumUser = true // Bool
print("\(name) is \(age) years old.")
print("Rating: \(rating), Premium: \(isPremiumUser)")String ကစာသား၊ Int က ကိန်းပြည့်၊ Double က ဒဿမကိန်း၊ Bool က true/false တန်ဖိုးပါ။ \(...) ကို string interpolation လို့ခေါ်ပြီး string ထဲမှာ variable value ထည့်ပြနိုင်ပါတယ်။
Nandar is 24 years old. Rating: 4.8, Premium: trueReal-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Data Types example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Data Types example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
let name: String = "Nandar" // Explicit type
let age = 24 // Int လို့ compiler ကခန့်မှန်းမယ်
let rating = 4.8 // Double
let isPremiumUser = true // Bool
print("\(name) is \(age) years old.")
print("Rating: \(rating), Premium: \(isPremiumUser)")ရလဒ်: Data Types ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Data Types example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Typed value → Swift expression / function → Safe result
- Data Types ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
- let age = 24 ပြီးနောက် age = "twenty four" လို့ assign လုပ်လို့မရပါ။ Type မတူလို့ error ဖြစ်ပါမယ်။
- Data Types example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
The Swift Programming Language — Swift.org