๐ŸŽ iOS/Swift

[Swift] Struct์™€ Class

๊ตฌ์กฐ์ฒด์™€ ํด๋ž˜์Šค ๊ธฐ๋ณธ ํ˜•ํƒœ

struct ๊ตฌ์กฐ์ฒด_์ด๋ฆ„{
    ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ๋“ค
}

class ํด๋ž˜์Šค_์ด๋ฆ„{
    ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ๋“ค
}

 

๊ตฌ์กฐ์ฒด์™€ ํด๋ž˜์Šค์˜ ๊ณตํ†ต์ 

  • ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ
  • ๋ฐ์ดํ„ฐ๋ฅผ ์šฉ๋„์— ๋งž๊ฒŒ ๋ฌถ์–ด ํ‘œํ˜„ํ•˜๊ณ ์ž ํ• ๋•Œ ์šฉ์ด
  • ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„๋‹ค
  • ์ดˆ๊ธฐํ™”๋ฅผ ์ •์˜ํ•˜์—ฌ ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค
  • ํ™•์žฅ(extension) ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ”„๋กœํ† ์ฝœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๊ตฌ์กฐ์ฒด์™€ ํด๋ž˜์Šค์˜ ์ฐจ์ด์ 

  • ๊ตฌ์กฐ์ฒด๋Š” value type, ํด๋ž˜์Šค๋Š” reference type
  • ๊ตฌ์กฐ์ฒด๋Š” ์ƒ์†ํ•  ์ˆ˜ ์—†๋‹ค.
  • ๊ตฌ์กฐ์ฒด๋Š” ์ƒ์„ฑ์ž๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์„ ์‹œ ๊ธฐ๋ณธ initializer๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํƒ€์ž… ์บ์ŠคํŒ…์€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—๋งŒ ํ—ˆ์šฉ ๋œ๋‹ค.

 

struct ์˜ˆ์‹œ

struct CharcterData{
    var name: String
    var age: Int
    
    // mutating์„ ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ, ๊ตฌ์กฐ์ฒด๋Š” value type์ด๊ธฐ ๋•Œ๋ฌธ์— method ์•ˆ์—์„œ property
    // ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ์— mutating์„ ๋ถ™์ด๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ๋‹ค์‹œ ์ƒ์„ฑ๋˜๋ฉด์„œ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
    mutating func updateUser(_ name: String, _ age: Int){
        self.name =.name
        self.age =.age
        
        print("์ด๋ฆ„:\(name), ๋‚˜์ด:\(age)")
    }
 }
 
 var lala = UserData(name: "๋ผ๋ผ", age: 17)
 print("์ด๋ฆ„: \(lala.name), ๋‚˜์ด:\(lala.age)")
 
 var mimi = lala 
 mimi.name = "๋ผ๋ผ" // ๋Œ€์ž…ํ•  ๋•Œ ๋ณต์‚ฌ๊ฐ€ ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ˆ˜์ •์ด ๋ฐœ์ƒํ•  ๋•Œ ๊ฐ’ ๋ณต์‚ฌ๊ฐ€ ์ผ์–ด๋‚จ
 mimi.age = 30
 
 
 

์ด ๋•Œ, mimi์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด๋„ lala์˜ ์†์„ฑ์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

class ์˜ˆ์‹œ

class Book{
    var name:String
    var coor: String
    
    // ์ด๋‹ˆ์…œ๋ผ์ด์ฆˆ๋ฅผ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.
    init(author:String, color:String){
        self.author = author
        self.tilte = title
    }
    
    // class๋Š” method ์•ˆ์—์„œ property ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— mutating ๋ถ™์ด์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
    func updateBook(author:String, title:String){
        self.author = author
        self.title = title
        
        print("์ €์ž: \(author), ์ œ๋ชฉ: \(title)")
    }
}

let HarryPotter = Book(author:"JKRolling",title:"HarryPotter")
print("์ €์ž: \(HarryPotter.author), ์ œ๋ชฉ: \(HarryPotter.title)")
// ์ €์ž: JKRolling, ์ œ๋ชฉ:HarryPotter

var Homes = HarryPotter
Homes.author = "abc"
Homes.title = "Homes"

print("์ €์ž: \(HarryPotter.author), ์ œ๋ชฉ: \(HarryPotter.title)")
// ์ €์ž: abc, ์ œ๋ชฉ: Homes
// ํด๋ž˜์Šค๋Š” reference type์ด๊ธฐ ๋•Œ๋ฌธ์— ์†์„ฑ์ด ํ•จ๊ป˜ ๋ณ€ํ˜•๋œ๋‹ค.

print("์ €์ž: \(Homes.author), ์ œ๋ชฉ: \(Homes.title)")
// ์ €์ž: abc, ์ œ๋ชฉ: Homes

Homes.updateBook(author:"JKRolling",title:"HarryPotter")
// ์ €์ž: JKRolling, ์ œ๋ชฉ:HarryPotter

 

๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๋•Œ

: ์• ํ”Œ ๊ถŒ์žฅ์‚ฌํ•ญ

 

1. ์—ฐ๊ด€๋œ ๊ฐ„๋‹จํ•œ ๊ฐ’์˜ ์ง‘ํ•ฉ์„ ์บก์Šํ™”ํ•˜๋Š” ๊ฒƒ๋งŒ์ด ๋ชฉ์ ์ผ ๋•Œ

2. ์ธ์Šคํ„ด์Šค๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด ํ•ฉ๋‹นํ•  ๋•Œ

3. ๊ตฌ์กฐ์ฒด์— ์ €์žฅ๋œ ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ฐ’ ํƒ€์ž…์ด๋ฉฐ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด ํ•ฉ๋‹นํ•  ๋•Œ

4. ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ๋ถ€ํ„ฐ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์†Œ๋“œ ๋“ฑ์„ ์ƒ์†๋ฐ›๊ฑฐ๋‚˜ ์ž์‹ ์„ ์ƒ์†ํ•  ํ•„์š”๊ฐ€ ์—†์„ ๋•Œ