BeginningDeveloperFeaturediOS DeveloperNewbieProgramming LanguageSwift

Fundamental พื้นฐานของภาษา Swift ตอนที่ 2

กลับมาที่เรื่องของ Fundamental ของภาษา Swift ที่สามารถพัฒนาได้ผ่าน XCode6 BETA ที่ทาง Apple Developer ได้ใช้กันก่อน เรามาศึกษาต่อเนื่องดีกว่า 

Screen Shot 2557-06-06 at 9.47.46 PM

คำสั่งทั้งหมดของ ภาษา  Swift ผมคง ใช้ Reference จากเว็บต่างประเทศมาลงให้ ศึกษากันเลยแล้วกันว่าหลักการ Syntax และโครงสร้างนั้นมันเขียนยังไง

ตัวแปร Array และการเขียนโปรแกรม ภาษา Swift

var colors = ["red", "blue"]
var moreColors: String[] = ["orange", "purple"] // explicit type
colors.append("green") // [red, blue, green]
colors += "yellow" // [red, blue, green, yellow]
colors += moreColors // [red, blue, green, yellow, orange, purple]

var days = ["mon", "thu"]
var firstDay = days[0] // mon
days.insert("tue", atIndex: 1) // [mon, tue, thu]
days[2] = "wed" // [mon, tue, wed]
days.removeAtIndex(0) // [tue, wed]

การเรียกใช้ Class ต่างๆ

class Counter {
  var count: Int = 0 
  func inc() { 
    count++
  }
  func add(n: Int) {
    count += n
  }
  func printCount() {
    println("Count: \(count)")
  }
}

var myCount = Counter()
myCount.inc()
myCount.add(2)
myCount.printCount() // Count: 3

การกำหนดเงื่อนไข Condition ของโปรแกรมภาษา Swift

IF THEN ELSE

let happy = true
if happy {
	println("We're Happy!")
} else {
	println("We're Sad :('")
}
// We're Happy!
let speed = 28
if speed <= 0 {
	println("Stationary")
} else if speed <= 30 {
	println("Safe speed")
} else {
	println("Too fast!")
}
// Safe speed

SWITCH CASE

let n = 2
switch n {
	case 1:
		println("It's 1!")
	case 2...4:
			println("It's between 2 and 4!")
	case 5, 6:
		println("It's 5 or 6")
	default:
		println("Its another number!")
}
// It's between 2 and 4!

การใช้ Dictionary

var days = ["mon": "monday", "tue": "tuseday"] 
days["tue"] = "tuesday" // change the value for key "tue"
days["wed"] = "wednesday" // add a new key/value pair

var moreDays: Dictionary = ["thu": "thursday", "fri": "friday"]
moreDays["thu"] = nil // remove thu from the dictionary
moreDays.removeValueForKey("fri") // remove fri from the dictionary

ตัวแปร Enums

enum CollisionType: Int { 
 case Player = 1 
 case Enemy = 2 
} 
var type = CollisionType.Player

การใช้ Loop For

for var index = 1; index < 3; ++index {
	// loops with index taking values 1,2 
}
for index in 1..3 {
	// loops with index taking values 1,2
}
for index in 1...3 { 
	// loops with index taking values 1,2,3
}

let colors = ["red", "blue", "yellow"]
for color in colors {
    println("Color: \(color)")
}
// Color: red
// Color: blue
// Color: yellow 

let days = ["mon": "monday", "tue": "tuesday"]
for (shortDay, longDay) in days {
    println("\(shortDay) is short for \(longDay)")
}
// mon is short for monday
// tue is short for tuesday

การประกาศฟังก์ชัน

func iAdd(a: Int, b: Int) -> Int {
  return a + b
}
iAdd(2, 3) // returns 5

func eitherSide(n: Int) -> (nMinusOne: Int, nPlusOne: Int) {
  return (n-1, n+1)
}
eitherSide(5) // returns the tuple (4,6)

หวังว่าคงช่วยเหลือนักพัฒนามือใหม่ได้บ้างนะครับ

Ref: Apple Developer

 

 

 

 

 

 

 

 

 

 

 

 

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Back to top button
Game & Mobile Development AR VR XR
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน