๐Ÿฆ… ํ˜ผ์žํ•˜๋Š” Swift ๊ณต๋ถ€ -1-

๐Ÿฆ… ํ˜ผ์žํ•˜๋Š” 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- ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

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