๐งโ๐ป ๋ชจ๊ฐ์ฝ 1์ฃผ์ฐจ
๐ ์ค๋์ ํ ์ผ
- ์ฃผ๋์ด ์ฐฝ์ ๊ฒฝ์ง๋ํ ์ฃผ์ ์ ํ๊ธฐ
- Swift ๊ธฐ๋ณธ ๋ฌธ๋ฒ ์ตํ๊ธฐ
๐ฃ ์ฃผ๋์ด ์ฐฝ์ ๊ฒฝ์ง๋ํ ์ฃผ์ ์ ํ๊ธฐ
๋ชจ๊ฐ์ฝ ํ์๋ค๊ณผ ํจ๊ป ์ฃผ๋์ด ์ฐฝ์ ๊ฒฝ์ง๋ํ์ ์ฐธ์ฌํ๊ธฐ๋ก ํ์๋ค. ์ฌ๋ฌ๊ฐ์ง ์ฃผ์ ๋ค์ ์๋ ผํ ๋์ ์ ๊ทผ์ฑ์ด ์ข๊ณ ํ๋ถ 2ํ๋ ์์ค์์ ๊ตฌํ์ด ๋น๊ต์ ๊ฐ๋จํ ์น ์๋น์ค๋ฅผ ๋ง๋ค์ด๋ณด๊ธฐ๋ก ์๊ฒฌ์ ๋ชจ์๋ค. ํ์๋ค๊ณผ ๋ค์ํ ์์ด๋์ด๋ฅผ ์๋ ผํ ๋์ ์์ ๊ธฐ๋ฅ์ด ์ฒจ๊ฐ๋ ์น ๊ธฐ๋ฐ์ ์์ ํ๋ ์ด์ด๋ฅผ ๋ง๋ค์ด ๋ณด๊ธฐ๋ก ํ์๋ค.
๐ Swift
์ด๋ฒ ์๊ฐ์ ์ ํ์ ๋์ ์ฑ์ ๋ฌด๋ฃ๋ก ์ ๊ณต๋๋ The Swift Programming Language (Swift 5.1)์ ํ ๋๋ก Swift์ ๊ธฐ๋ณธ ๋ฌธ๋ฒ์ ๊ณต๋ถ๋ฅผ ์์ํ์๋ค. ์๋์ ๋ด์ฉ๋ค์ ์ค๋ ๊ณต๋ถํ ๊ฐ๋ ๊ณผ ์ค์ต ์ฝ๋, ๊ฒฐ๊ณผ๋ฌผ์ด๋ค. ๊ณต๋ถ์ ๋ด์ฉ์ ์ฐจํ์ ๋ณ๋๋ก ์ ๋ฆฌํ์ฌ ๋ธ๋ก๊ทธ์ ๋ค์ ํฌ์คํ ํ ์์ ์ด๋ค.
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!
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" : "KNCCS",
"seunghun" : "CERT"
]
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โ: โKNCCSโ, โseunghunโ: โCERTโ] [โminseokโ: โKNCCSโ, โseunghunโ: โPC Managementโ]
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
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
optional์ ๊ฐ์ด ์์์๋ ์์์๋ ์์ ๋ ์ฌ์ฉํ๋ค. swift์์ ์ฌ์ฉ๋๋ nil์ ๋ค๋ฅธ ์ธ์ด์์ null์ ๊ฒฝ์ฐ์ ๋์ํ๋๋ฐ, swift์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ด ์์ ๊ฐ๋ฅ์ฑ์ด ์์์ ํ์ค์ ํ๊ณ , ์์ธ์ ์ผ๋ก ๊ฐ์ด ์์ ์ ์๋ ๋ถ๋ถ์ 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
๋ช
์์ ์ผ๋ก 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 1, new tallest height is 188 tallest height found in 2, new tallest height is 192
for๋ฌธ์ผ๋ก iterateํ ๋ ๋๋คํ๊ฒ ๋ฐฉ๋ฌธํ๋ค. ๋งค๋ฒ ์คํํ ๋ ๋ง๋ค ์ด๋ค ํค๊ฐ๋จผ์ ํ์ธํ๋๊ฐ ๋ฌ๋ผ์ง
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
0๋ถํฐ .. 10๊น์ง ๋ฅผ ํํํ๋ ค๋ฉด 0..<11๋ก ํ๋ฉด ๋๋ค. ์ด์ํ๊ฒ โค๋ ์ฌ์ฉ์ด ์๋๋ค.
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
๋๊ฐ์ง ํํ๋ก ์ ์ธ๋ ์ ์์. ์ธ์ ์์ _๋ฅผ ์๋ถ์ด๋ฉด ํธ์ถํ ๋์๋ ์ธ์ ์ด๋ฆ์ ๋ช
์ํด์ฃผ์ด์ผํจ. ๊ทธ๋ฐ ๋ถํธํจ์ ํผํ๋ ค๋ฉด ์ธ์ ์์ _๋ฅผ ๋ฃ์ด์ ์ ์ธํด์ฃผ๋ฉด ๋๋ค.
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
๋ฆฌํด ํ์
์ ์ ํ ์ ์๋ค. ์์ ๊ฒฝ์ฐ dictionary๋ก ๋ฐํํ๋๊ฒ. ๊ทธ๋์ ๋ฐํ๋ฐ์ ๊ฐ์ .์ฐ์ฐ์๋ก ์ ๊ทผํ ์ ์๋ค.
๋ค์ ๊ธ ๐งโ๐ป ๋ชจ๊ฐ์ฝ 2์ฃผ์ฐจ๋ณด๋ฌ๊ฐ๊ธฐ.
Comments