Thuta Learning
ရှာဖွေရန်
BasicProgrammingbeginner

Data Types

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Swift က type-safe language ဖြစ်ပါတယ်။ ဆိုလိုတာက variable တစ်ခုမှာ ဘယ် data type သိမ်းမလဲဆိုတာကို compiler က တိတိကျကျသိထားရပါတယ်။ ဒါကြောင့် number ထဲကို text ထည့်တာ၊ text ထဲကို boolean ထည့်တာလို mistake တွေကို စောစောဖမ်းပေးနိုင်ပါတယ်။

Swift မှာ type ကို ကိုယ်တိုင်ရေးနိုင်သလို compiler က တန်ဖိုးကြည့်ပြီး ခန့်မှန်းပေးတဲ့ type inference လည်းရှိပါတယ်။

swift
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 ထည့်ပြနိုင်ပါတယ်။

You should see
Nandar is 24 years old. Rating: 4.8, Premium: true

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • let age = 24 ပြီးနောက် age = "twenty four" လို့ assign လုပ်လို့မရပါ။ Type မတူလို့ error ဖြစ်ပါမယ်။
Data Types | Thuta Learning