8์ผ์ฐจ: 100DaysOfSwift๐Ÿš€

3277 ๋‹จ์–ด reactnativeswiftiosjavascript
8์ผ์ฐจ
๊ตฌ์กฐ์ฒด ํŒŒํŠธ 1
๋‚˜๋งŒ์˜ ๊ตฌ์กฐ์ฒด ๋งŒ๋“ค๊ธฐ

Swift์—์„œ ๊ตฌ์กฐ์ฒด๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์˜ ๋ณ€์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๊ตฌ์กฐ์ฒด๋Š” "๋ช…๋ช…๋œ ์œ ํ˜•"์ž…๋‹ˆ๋‹ค. String, Int ๋˜๋Š” Array์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ณ ์œ ํ•œ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜์—ฌ ๋‚˜์ค‘์— ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•  ๋ช…๋ช…๋œ ์œ ํ˜•์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ตฌ์กฐ์ฒด ๋‚ด๋ถ€์˜ ๋ณ€์ˆ˜๋ฅผ ์†์„ฑ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๊ตฌ์กฐ์ฒด๋Š” ๊ณ ์œ ํ•œ ๋ณ€์ˆ˜์™€ ์ƒ์ˆ˜, ๊ณ ์œ ํ•œ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ ๋‹ค์Œ ์›ํ•˜๋Š” ๋Œ€๋กœ ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

struct Sport {
   var name: String
}


์œ ํ˜•์„ ์ •์˜ํ•˜๋ฏ€๋กœ ์ด์ œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

var tennis = Sport(name: "Tennis")
print(tennis.name)

Output:
Tennis

tennis.name = "Lawn tennis"
print(tennis.name)

Output:
Lawn tennis


์†์„ฑ์€ ์ผ๋ฐ˜ ๋ณ€์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ธฐ๋ณธ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ Swift์˜ ์œ ํ˜• ์ถ”๋ก ์— ์˜์กดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ณ„์‚ฐ๋œ ์†์„ฑ

struct Sport {
    var name: String
}


์—ฌ๊ธฐ์—๋Š” String์„ ์ €์žฅํ•˜๋Š” name ์†์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Swift์—๋Š” ๊ฐ’์„ ์•Œ์•„๋‚ด๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์†์„ฑ์ธ ๊ณ„์‚ฐ๋œ ์†์„ฑ์ด๋ผ๋Š” ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์†์„ฑ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ €์žฅ ์†์„ฑ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

struct Sport {
    var name: String
    var isOlympicSport: Bool

    var olympicStatus: String {
        if isOlympicSport {
            return "\(name) is an Olympic sport"
        } else {
            return "\(name) is not an Olympic sport"
        }
    }
}

let chessBoxing = Sport(name: "Chessboxing", isOlympicSport: false)
print(chessBoxing.olympicStatus)

Output:
Chessboxing is not an Olympic sport


์žฌ์‚ฐ ๊ด€์ฐฐ์ž

struct Progressa {
    var task: String
    var amount: Int {
        didSet {
            print("\(task) is now \(amount)% complete")
        }
    }
}


var progress = Progressa(task: "Loading data", amount: 0)
progress.amount = 30
progress.amount = 80
progress.amount = 100

Output:
Loading data is now 30% complete
Loading data is now 80% complete
Loading data is now 100% complete


์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ Swift๊ฐ€ ๊ธˆ์•ก์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด didSet ์†์„ฑ ๊ด€์ฐฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ธˆ์•ก์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ผ๋ถ€ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์†์„ฑ์ด ๋ณ€๊ฒฝ๋˜๊ธฐ ์ „์— ์กฐ์น˜๋ฅผ ์ทจํ•˜๊ธฐ ์œ„ํ•ด willSet์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๊ฑฐ์˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ํ–‰๋™ ์–‘์‹

๊ตฌ์กฐ์ฒด๋Š” ๋‚ด๋ถ€์— ํ•จ์ˆ˜๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋Š” ํ•„์š”์— ๋”ฐ๋ผ ๊ตฌ์กฐ์ฒด์˜ ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์กฐ์ฒด ๋‚ด๋ถ€์˜ ํ•จ์ˆ˜๋ฅผ ๋ฉ”์„œ๋“œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

struct City {
    var population: Int

    func collectTaxes() -> Int {
        return population * 1000
    }
}

let london = City(population: 9_000_000)
london.collectTaxes()


๋Œ์—ฐ๋ณ€์ด ๋ฐฉ๋ฒ•

๊ตฌ์กฐ์ฒด์— ๋ณ€์ˆ˜ ์†์„ฑ์ด ์žˆ์ง€๋งŒ ๊ตฌ์กฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์ˆ˜๋กœ ์ƒ์„ฑ๋œ ๊ฒฝ์šฐ ํ•ด๋‹น ์†์„ฑ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด mutating ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

struct Person {
    var name: String

    mutating func makeAnonymous() {
        name = "Anonymous"
    }
}

var person = Person(name: "Ed")
person.makeAnonymous()

print(person.name)

Output:
Anonymous


๋ฌธ์ž์—ด์˜ ์†์„ฑ ๋ฐ ๋ฉ”์„œ๋“œ

let string = "Welcome to 100Days Of Swift"

print(string.count)  //27


๋ฌธ์ž์—ด์ด ํŠน์ • ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” hasPrefix() ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

print(string.hasPrefix("Sau")) //false

print(string.hasPrefix("Welcome")) //true

print(string.uppercased()) //WELCOME TO 100DAYS OF SWIFT

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ