เขียนโปรแกรมภาษา swift เบื้องต้น กับโปรแกรมเมอร์ที่เด๋อที่สุดในประวัติศาสตร์ ♥

สวัสดีคะวันนี้จะมาสอนภาษา swift เบื้องต้นกันนะคะ ภาษา Swift พื้นฐานมาจาก C
และ Objective C ซึ่งเป็นการเขียนแอพ , โปรแกรมบน iOS ภาษา swift จะไม่มี [;]
(semicolon) ปิดท้ายประโยค แต่ก็ยังสามารถใช้ได้โดยไม่ติด Error ถ้าหากยังติดมา
จาก C อยู่คะ แต่ Swift นี่จะนับจากการเว้นวรรค ถ้าเว้นบ้างไม่เว้นบ้างก็จะเกิด Error
คะ ดังนั้นเราควรจะเว้นก็เว้นไปเลย ไม่เว้นก็ไม่เว้นสักตัวเลย

วิธีการปริ้น เช่น ประกาศตัวแปร


Comment คือข้อความที่ใช้อธิบายโค้ด ให้ตนเองหรือผู้อ่านเข้าใจในโค้ด ซึ่งถ้าเรา Compliler จะไม่เกิดค่า comment ปรากฎบนจอ comment ใน Swift นั้นจะเหมือนกับภาษา C และ Objective C
Comment แบบบรรทัดเดียว ใช้ //…………………….
Comment แบบหลายบรรทัด ใช้ /*…………………..
……………………………………………….*/

การประกาศตัวแปลมี 2 ประเภท
Type Safe เป็นแบบระบุชนิดตัวแปลเข้าไปเลย เช่น
let intro : Int = 20
2. Type Inference แบบไม่ระบุชนิดตัวแปล เช่น
let intro = 20
โดยส่วนมากจะนิยมใช้แบบที่ 1 Type Safe มากกว่า เพราะจะได้รู้ชนิดของตัวแปลนั้นๆ ไม่เกิดการสับสน และการประกาศตัวแปล จะเริ่มต้นด้วยตัวพิมพ์ใหญ่เสมอ เช่น Int ,Float,Double
Swift จะมีการประกาศตัวแปรเป็น2แบบ คือ
let (Constant) เป็นตัวแปลที่เปลี่ยนแปลงค่าไม่ได้ จะใช้เก็บค่าคงที่ โดยที่ค่าคงที่นี้จะไม่เปลี่ยนแปลง
var (Variables) เป็นตัวแปลที่เปลี่ยนแปลงค่าได้ และมีหลายค่าได้

String -ตัวอักษรหลายตัว “Hello”
Character -ตัวอักษรเพียวตัวเดียว “C”
Float -ทศนิยมได้ถึง 6 หลัก
Double -ทศนิยมได้ถึง 15หลัก
Int -จำนวนเต็มทั้งติดลบและไม่ติดลบก็ได้
UInt -จำนวนเต็มที่ไม่สามารถติดลบได้ หรือไม่ต้องการให้ค่าติดลบ
Bool -True,False
Optional -เป็นชนิดตัวแปลที่จะเก็บค่าที่มีค่าหรือไม่มีค่าก็ได้

ตัวแปลแบบ Optional จะมีค่าก็ได้ หรือไม่มีค่าก็ได้ ถ้าประกาศตัวแปลโดยเพิ่ม [?]ต่อจากชนิดข้อมูล เช่นจาก Int ถ้าเป็น Optional ก็จะเป็น Int? เช่น
let intro : Int? = 20 ถ้าจะใช้ค่าของตัว Optional ที่มี [?] ต้องใช้ [!]หลังชื่อตัวแปลที่นำไปใช้ เช่น print(intro!)
หรือไม่ก็ใส่ [!] ในชนิดข้อมูลแทน [?] เลย เช่น let intro : Int! = 20 จะทำให้ไม่ต้องใส่ [?] ตอนประการตัวแปล และไม่ต้องใส่ [!] ตอนเรียกใช้ตัวแปล เช่น print(intro)
ถ้าจะทำให้ค่า Optional ไม่มีค่า (nil)
ถ้าเราไม่ตั้งค่าให้ Optional มันกำหนดค่าเป็น nil ให้โดยอัตโนมัติ หรือทำให้ไม่มีค่าโดยกำหนดค่า nil เช่น optionalInt = nil

Integer Literals
let decimalInteger = 17 (เลขฐาน 10)
let binaryInteger = 0b10001 (เลขฐาน 2)
let octalInteger = 0o21 (เลขฐาน 8)
let hexadecimalInteger = 0x11 (เลขฐาน16)
String literals
\0 ไม่มีตัวอักษรต่อไปหรือค่าว่างเปล่า
\\ เพื่อโชว์ \
\b ลบ (Backspace)
\n ขึ้นบรรทัดใหม่
\t เว้นวรรค
\’ ,\” เพื่อโชว์ (‘ , “)

A = 10 : B = 20
[+]เครื่องหมายบบวก เช่น A + B = 30
[ -] เครื่องหมายลบ เช่น A -B = -10
[*] เครื่องหมายคูณ เช่น A * B =200
[/] เครื่องหมายหาร เช่น A / B =2
[ % ] เครื่องหมาย Mod เช่น A % B = 0
[++] บวกเพิ่มไปทีละ 1 = 11
[- -] ลบเพิ่มไปทีละ 1 = 9

A = 10 ; B = 20
[==] เท่ากับ, เท่ากับ เช่น A == B ผิดเพราะ Aมีค่าเท่ากับ 10; B มีค่าเท่ากับ 20
[ != ] ไม่เท่ากับ เช่น A != B ถูกต้องเพราะ A = 10 ; B = 20
[ > ] มากกว่า เช่น A > B ผิด
[ < ] น้อยกว่า A < B ถูก
[ > = ] มากกว่าเท่ากับ A >= B ผิด
[ < = ] น้อยกว่าเท่ากับ A <= B ถูก
Logical Operators
A = 1 ; B = 0
[ && ] = And เช่น A && B =เป็นเท็จ
[ || ] = Or เช่น A || B = เป็นจริง
[ ! ] = Not เช่น !A&&B = เป็นจริง

vae1. if statement
var varA : Int = 10
if varA < 20 { ………………………………….……..ถ้า varA น้อยกว่า 20
print(“varA is less than 20”) ……………….……….ปริ้น
}
print(“value of variable varA is \(varA)”) …………….ปริ้นค่า varA
…………………………………………..
varA is less than 20
value of variable varA is 10
………………………………………….
2. if…else statement
var varA : Int = 100
if varA <20 { ………………………………ถ้า varA น้อยกว่า 20
print(“varA is less than 20") }……………..ปริ้น
else{ …………………………………………แต่ถ้า varA ไม่น้อยกว่า20
print(“varA is not less than 20”) ………..…ปริ้น
}
print(“value of variable varA is \(varA)”) ………….ปริ้นค่า varA
……………………………………….
varA is not less than 20
value of variable varA is 100
……………………………………….
3. if…else if…else statement
var varA : Int = 100
if varA == 20 { …………………………………………..ถ้า varA เท่ากับ 20
print(“varA is equal to than 20”) }………………………ปริ้น
else if varA == 50 { ……………………………………..แต่ถ้า varA เท่ากับ 50
print(“varA is equal to than 50”) }……………………..ปริ้น
else { ……………………………………………….และ varA ไม่เท่ากับ 20 และ 50
print(“None of the values is matching”) } …………………..ปริ้น
print(“value of variable varA is \(vaeA)”) …………………..ปริ้นค่า varA
……………………………………….
None of the values is matching
value of variable varA is 100
………………………………………..
4. Nested If Statements
var varA : Int = 100
var varB : Int = 200
if varA == 100 { …………………………………ถ้า varA เท่ากับ100
print(“First Condition is satisfied”)………………ปริ้น
if varB == 200 {…………………………………ถ้า varB เท่ากับ200
print(“Second Condition is also satisfied”) }}……ปริ้น
print(“value of variable varA is \(varA)”) ……………..ปริ้นแสดงค่า varA
print(“value of variable varB is \(varB)”) ……………..ปริ้นแสดงค่า varB
…………………………………………
First Condition is satisfied
Second Condition is also satisfied
value of variable varA is 100
value of variable varA is 200
………………………………………….
5. Switch Statements
var index = 10
switch index {
case 100 :
print(“value of index is 100”)
case 10,15 :…………………………………….index เท่ากับ10
print(“value of index is either 10 or 5”)……….ปริ้น
case 5 :
print(“value of index is 5”)
default :…………………………………………ไม่มีcaseไหนถูก ก็จะเข้า default
print(“default case”) } …………………………ปริ้น
………………………………………
value of index is either 10 or 5
………………………………………

1. for -in Loop
var someInts : [Int] = [10, 20, 30]
for index in someInts {
print(“value of index is \(index)”) }
……………………………………
value of index is 10
value of index is 20
value of index is 30
……………………………………..
2.While Loop
var index = 10
while index < 20 {
print(“value of index is \(index)”)
index = index + 1 }
……………………………………
value of index is 10
value of index is 11
value of index is 12
value of index is 13
value of index is 14
value of index is 15
value of index is 16
value of index is 17
value of index is 18
value of index is 19
………………………………..
3. do…while Loop
var index = 10
do {
print(“value of index is \(index)”)
index = index + 1 }
} while index < 20
……………………………………
value of index is 10
value of index is 11
value of index is 12
value of index is 13
value of index is 14
value of index is 15
value of index is 16
value of index is 17
value of index is 18
value of index is 19
………………………………..
วีดีโอสอนภาษา swift
