๐ฆ ํผ์ํ๋ Swift ๊ณต๋ถ -1-
๐ Swift ๊ณต๋ถ..
iOS์ macOS ์ฑ ๊ฐ๋ฐ์ ๋ชฉํ๋ก ์ด๋ฒ ๋ฐฉํ(2020๋ ๊ฒจ์ธ๋ฐฉํ) Swift๊ณต๋ถ๋ฅผ ๊ณํํ๋ค. ๋ฐฉํ์ ๋๊ธฐ๋ค๊ณผ ํจ๊ป ์งํํ๋ ๋ชจ๊ฐ์ฝ์ ํจ๊ป ๊ณต๋ถ๋ฅผ ์์ํ๋ค. ๋ชจ๊ฐ์ฝ์์ ๊ณต๋ถํ ๋ด์ฉ์ ์ ๋ฆฌํด์ ์ฌ๋ฆด ์์ ์ด๋ค.
์ ํ์ ๋์์ฑ์ ์ ๊ณต๋๋ Swift ๊ธฐ๋ณธ ์์ ์ธ The Swift Programming Language (Swift 5.1)๋ฅผ ์ฐธ๊ณ ํ๋ฉฐ ๊ณต๋ถํ๋ค.
Let, Var, Print
let label = "area is "
let width: Double = 30 //let์ constant, var์ variable
var height = 20.5 //name: datatype = value๋ก ๋ช
์์ ์ผ๋ก ํ์ ๋ํ๋ด์ค ์ ์๋ค.
print(label + String(width * height)) //int๋ int๋ผ๋ฆฌ๋ง ์ฐ์ฐ ๊ฐ๋ฅํ๋ฏ...?
height = 40
print(label + String(width * height)) //์ถ๋ ฅํ ๋ String์ผ๋ก ํ๋ณํ์ด ๋์ด์ผํจ.
print(label + "\(width * height) square meter") //backslash๋ฅผ ํตํด ๋ฌธ์์ด ์์ ๋ณ์๋ฅผ ๋ฃ์ ์ ์๋ค.
let numberOfOranges = 3
let numberOfApples = 5
let comment = """
I got \(numberOfOranges) oranges,
and \(numberOfApples) apples,
so I got total \(numberOfOranges + numberOfApples) fruits!
"""
print(comment)
area is 615.0 area is 1200.0 area is 1200.0 square meter
I got 3 oranges, and 5 apples, so I got total 8 fruits!
Swift๋ Python์ฒ๋ผ main์ด ๋ฐ๋ก ํ์ํ์ง ์๋ค. ์ฝ๋ ์ค ๋์ ;์ ์จ ์ค ํ์๋ ์๋ค. letํค์๋๋ก ์์๋ฅผ varํค์๋๋ก ๋ณ์๋ฅผ ์ ์ธํ ์ ์๊ณ , ์๋ฃํ์ ๋ช
์ํด์ฃผ์ง ์์๋ ์ด๊ธฐํ๋ ๊ฐ์ ํตํด ์ถ์ ๋ ์๋ฃํ์ ๊ฐ๊ฒ๋๋ค. ๋ฌธ์์ด์ ๊ฒฝ์ฐ """ ๋๊ฐ ์ฌ์ด์ ์ฌ๋ฌ์ค์ ๋ฃ์ด์ค ์ ์๋ค. printํจ์๋ฅผ ํตํด ์ถ๋ ฅํ ์ ์๋๋ฐ, ์ธ์๋ก ์ ๋ฌํ ๋ ๋ณ์๋ ์์๋ \()๋ฅผ ํตํด ๋ฌธ์์ด๊ณผ ํจ๊ป ์ถ๋ ฅํ๋๋ก ํ ์ ์๋ค.
List, Dictionary
var shoppingList = [ "apple", "orange", "garlic", ]
print(shoppingList)
shoppingList[2] = "onion"
print(shoppingList)
shoppingList.append("bannana")
print(shoppingList)
print("2nd thing I have to buy is " + shoppingList[2] + "\n");
var professionList = [
"minseok" : "Electrical Engineering",
"seunghun" : "Computer Science Engineering"
]
print(professionList)
professionList["seunghun"] = "PC Management"
print(professionList)
[โappleโ, โorangeโ, โgarlicโ] [โappleโ, โorangeโ, โonionโ] [โappleโ, โorangeโ, โonionโ, โbannanaโ] 2nd thing I have to buy is onion
[โminseokโ: โElectrical Engineeringโ, โseunghunโ: โComputer Science Engineeringโ] [โminseokโ: โElectrical Engineeringโ, โseunghunโ: โPC Managementโ]
python๊ณผ ๋์ผํ๊ฒ list์ dictionary๋ฅผ ์ ์ธํ ์ ์๋ค. ์ธ๋ฑ์ค๋ 0๋ถํฐ ์์ํ๋ค.
For
var scoreList = [97, 88, 0, 25, 43, 63, 55, 72, 38]
var totalScore = 0
for score in scoreList{
totalScore += score
}
print("you got total \(totalScore) score")
print("your average score is \(totalScore / scoreList.endIndex)") //end index equals to the size of list
you got total 481 score your average score is 53
for๋ฌธ์ ์คํ์ผ๋ python๊ณผ ์ ์ฌํ๋ค.
Optional
var optional: String? = "seunghun"
//optional = nil
if let name = optional{
print("\nHello, \(name)")
}
else{
print("\noptional is nil")
}
var nickName: String? = "yabby"
var fullName = "Seunghun Yang"
print("Hi, \(nickName ?? fullName)") //conditional print
Hello, seunghun Hi, yabby
Swift๋ฅผ ๊ณต๋ถํ๋ฉฐ ์ฒ์ ์ ํ๊ฒ๋ ๊ณ๋
์ด์๋ค. optional์ ๊ฐ์ด ์์์๋(nil, ๋ค๋ฅธ์ธ์ด์์ null์ผ ์๋) ์๋ค ๋ผ๋๊ฒ์ ๋ช
์ํด ์ฃผ๋ ๊ฒ์ธ๋ฐ, ์ด๋ ๊ฒ ํจ์ผ๋ก์จ optional์ด ์๋ ์๋ฃํ์ ๊ธฐ๋ณธ์ผ๋ก ์ฌ์ฉํ์ฌ ๊ฐ์ด ์์ ์ ์๋ค๋๊ฒ์ ์ ์ ๋ก ํ๋ค๋ ๊ฒ์ด๋ค. nullPointException์ ๋ง์ ์ ์๋ค๊ณ ํ๋ค. optional์ ๋ณ์์ ์๋ฃํ์ ๋ช
์ํ ๋ ๋ค์ ?์ ๋ถ์ฌ์ ๋ช
์ํด์ค ์ ์๋ค. ์์ ์ฝ๋๋ ์ด๋ฆ์ด ์์๊ฒฝ์ฐ์ ์์๊ฒฝ์ฐ๋ฅผ ๋ค๋ฅด๊ฒ ์ฒ๋ฆฌํด์ฃผ๋๋ก ํ๊ณ , ๋ณ๋ช
์ด ์๋ค๋ฉด ๋ณ๋ช
์ผ๋ก, ์๋ค๋ฉด ์ด๋ฆ์ผ๋ก ์ธ์ฌ๋ฅผ ์ถ๋ ฅํ๋๋ก ํ ๊ฒ์ด๋ค.
Switch
var vegetable = "bacon"
switch vegetable{ //no need to break for separating each cases
case "cucumber":
print("I hate cucumber")
case "lettuce", "tomato", "bacon": //, works like or
print("\nLet's make some BLT sandwiches")
default:
print("\nLet's go get some") //default is needed
}
Letโs make some BLT sandwiches
๋ค๋ฅธ ์ธ์ด์ switch๋ฌธ๊ณผ ํฌ๊ฒ ๋ค๋ฅธ๊ฒ์ ์์ง๋ง ๋ช
์์ ์ผ๋ก ๊ฐ case๋ง๋ค break๋ฅผ ํด ์ค ํ์๊ฐ ์๋ค๋๊ฒ์ด ํน์ง์ด๋ค.
Dictionary Iteration
var classInfo = [
1 : [188, 172, 165, 157],
2 : [192, 183, 171, 162],
3 : [165, 168, 173, 176]
]
var tallestHeight = 0
for (classNumber, heights) in classInfo{
for height in heights{
//print("finding \(classNumber)") //search begins random point
if height > tallestHeight{
tallestHeight = height
print("tallest height found in \(classNumber), new tallest height is \(height)")
}
}
}
tallest height found in 3, new tallest height is 165 tallest height found in 3, new tallest height is 168 tallest height found in 3, new tallest height is 173 tallest height found in 3, new tallest height is 176 tallest height found in 2, new tallest height is 192
for๋ฌธ์ ํตํด ์ํํ ๋์๋ ๋๋คํ ์์๋ก ์ ๊ทผํ๋ค. ์คํํ ๋๋ง๋ค ์ฐพ์์ง๋ ์์๊ฐ ๋ฌ๋ผ์ง๊ณ ๊ฒฐ๊ณผ๋ ๋ฌ๋ผ์ง๋ค.
Loops
var i = 0
while i < 9{
print("Hello \(i)")
i += 1
}
print()
var sum = 0
for i in 0..<11{ //can't use <=
sum += i
}
print("sum of 0 to 10 is \(sum)")
Hello 0 Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8
sum of 0 to 10 is 55
..<ํค์๋๋ก ์์๋ถ๋ถ๊ณผ ๋๋๋ ๋ถ๋ถ์ ์ ํด ๋ฐ๋ณต์ ๋ฒ์๋ฅผ ์ ํด์ค ์ ์๋ค. ๋ฐ๋ณต๋ฌธ์ ๊ตฌ์กฐ๋ python๊ณผ ๋ฎ์์๋ค.
Function
func greet(fullName: String, nickName: String?){
print("Hello, \(nickName ?? fullName)")
}
func greet2(_ fullName: String, _ nickName: String?){
print("Hello, \(nickName ?? fullName)")
}
greet(fullName : "seunghun", nickName: nil) //must use labels with parameter when call function
greet2("kiyoung", "kkiyo") //when you declare func, if you add _ before parameter, you can call without labels
Hello, seunghun Hello, kkiyo
func๋ฅผ ํตํด ํจ์๋ฅผ ์ ์ํ ์ ์๊ณ , ๋งค๊ฐ๋ณ์ ์ด๋ฆ๊ณผ ์๋ฃํ์ ๋ช
์ํด์ฃผ์ด์ผํ๋ค. ํจ์๋ฅผ ํธ์ถํ ๋์๋ ์ธ์์ ๋งค๊ฐ๋ณ์ ์ด๋ฆ์ ๋ช
์ํด์ฃผ์ด์ผ ํ๋ค. ๋ง์ฝ ์ด๋ฅผ ์๋ตํ๊ณ ์ถ๋ค๋ฉด ์ ์ธ์์ ๋งค๊ฐ๋ณ์์ ์ด๋ฆ ์์ _ ๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด ๋๋ค.
Function Return
func scoreStatistics(_ scores: [Int]) -> (min: Int, max: Int, tot: Int, avg: Int){
var min = 100
var max = 0
var tot = 0
var avg = 0;
for score in scores{
tot += score
if score < min{
min = score
}
else if score > max{
max = score
}
}
avg = tot / scores.endIndex
return (min, max, tot, avg)
}
var scoresOfMidterm = [97, 66, 57, 87, 33, 29]
let midtermStatistics = scoreStatistics(scoresOfMidterm)
print("your midterm scores : \(scoresOfMidterm)")
print("midterm statistics : your best score is \(midtermStatistics.max), lowest score is \(midtermStatistics.min), total score is \(midtermStatistics.tot), averages score is \(midtermStatistics.avg)")
your midterm scores : [97, 66, 57, 87, 33, 29] midterm statistics : your best score is 87, lowest score is 29, total score is 369, averages score is 61
๋ฐํํ์ ํจ์์ ๋์ ->๋ฅผ ํตํด ๋ช
์ํด์ค ์ ์๋ค. ์์ ์์์์ scoreStatisticsํจ์๋ dictionary๋ฅผ ๋ฐํํ๊ณ ์๋ค.
๋ค์ ๊ธ ๐ ํผ์ํ๋ Swift ๊ณต๋ถ -2- ๋ณด๋ฌ๊ฐ๊ธฐ
Comments