๐Ÿง‘โ€๐Ÿ’ป ๋ชจ๊ฐ์ฝ” 1์ฃผ์ฐจ

๐Ÿง‘โ€๐Ÿ’ป ๋ชจ๊ฐ์ฝ” 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์ฃผ์ฐจ๋ณด๋Ÿฌ๊ฐ€๊ธฐ.

Share: Twitter Facebook
Seunghun Yang's Picture

About Seunghun Yang

Seunghun is undergraduate student at Computer Science Engineering in CNU(Chungnam National University).

Daejeon, South Korea

Comments