๐Ÿ“ฑ App Development Study/iOS๐ŸŽ

[Udemy iOS & Swift Bootcamp] Swift Structures, Access Levels, Tuples

ibelieveinme 2023. 7. 27. 15:54
728x90

* Structures vs Classes

1) Struct ๊ธฐ๋ณธ๊ตฌ์กฐ

struct MyStruct {
       var aProperty: String

       func aMethod() {
               // Do Something
       }
}

 

2) struct ์™€ calss ๊ณตํ†ต์ ๊ณผ ์ฐจ์ด์ 

  struct class
๊ณตํ†ต์  ์ด๋ฆ„ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘.
์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ.
์ฐจ์ด์  ์ƒ์†๋ฐ›๊ธฐ ์•ˆ๋จ

stack ์— ์ €์žฅ
(๊ฐ€์žฅ ์ตœ๊ทผ ๋ฐ์ดํ„ฐ์— ๋น ๋ฅด๊ฒŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ)

vlaue ํ˜•์‹(๊ฐ’์ด ๋ณต์‚ฌ๋จ)
์ƒ์†๋ฐ›๊ธฐ ๋จ

Heap ์— ์ €์žฅ
(์ฐธ์กฐ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ’์„ ์ฐพ์Œ)

reference ํ˜•์‹(์ฃผ์†Œ๊ฐ€ ๋ณต์‚ฌ๋จ)

ํœด๋Œ€ํฐ์€ ์ ‘๊ทผ์ด ์‰ฌ์šด ๊ฐ€๋ฒผ์šด RAM ๋ฉ”๋ชจ๋ฆฌ์— Stack, Heap ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•จ.

 

3) ์˜ˆ์‹œ์ฝ”๋“œ

ClassHero ํด๋ž˜์Šค์™€ StructHero ๊ตฌ์กฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ๊ฐ name, universe ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜์ž.

//ClassExample.swift

import Foundation

class ClassHero {
    var name: String
    var universe: String
    
    init(name: String, universe: String){
        self.name = name
        self.universe = universe
    }
}
//StructExample.swift

import Foundation

struct StructHero {
    
    var name: String
    var universe: String
}

๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ class ์™€ struct, enum ์„ ์—ด๊ฑฐํ•  ์ˆ˜ ์—†๋‹ค.

ํด๋ž˜์Šค์— ์†์„ฑ์„ ๋ถ€์—ฌํ•˜๋ฉด initialize ๊ฐ€ ๊ตฌ๋ฌธ์ด ํ•„์š”ํ•˜๋‹ค.

struct์€ ์†์„ฑ์„ ๋ถ€์—ฌํ•ด๋„ initailize ๊ตฌ๋ฌธ์ด ํ•„์š”์—†๋‹ค. ์ฆ‰ initailize ๊ตฌ๋ฌธ ์—†์ด๋„ ๊ฐ์ฒด ์„ ์–ธ&์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

//main.swift

import Foundation

var hero = ClassHero(name: "Iron man", universe: "Marvel")
var anotherMarvelHero = hero

anotherMarvelHero.name = "The Hulk"

print("Hero name = \(hero.name)")
print("anotherMarvelhero name = \(anotherMarvelHero.name)")
Hero name = The Hulk
anotherMavelhero name = The Hulk

ClassHero ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ anotherMarvelHero ๋ณ€์ˆ˜์— ๋ณต์‚ฌํ–ˆ๋”๋‹ˆ ๊ฐ™์€ "The Hulk" ๊ฐ’์ด ๋‚˜์™”๋‹ค.

์ด์œ ๋Š” Class ๋Š” ๊ฐ’ ๋ณต์‚ฌ๊ฐ€ ์•„๋‹Œ ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

import Foundation

var hero = StructHero(name: "Iron man", universe: "Marvel")
var anotherMarvelHero = hero

anotherMarvelHero.name = "The Hulk"

print("Hero name = \(hero.name)")
print("anotherMarvelhero name = \(anotherMarvelHero.name)")
Hero name = Iron man
anotherMavelhero name = The Hulk

Struct ๋กœ ๋งŒ๋“  StructHero ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

import Foundation

var hero = StructHero(name: "Iron man", universe: "Marvel")

var anotherMarvelHero = hero
anotherMarvelHero.name = "The Hulk"

var avengers = [hero, anotherMarvelHero]
avengers[0].name = "Thor"

print("Hero name = \(hero.name)")
print("anotherMarvelhero name = \(anotherMarvelHero.name)")
print("first avenger name \(avengers[0].name)")
Hero name = Iron man
anotherMarvelhero name = The Hulk
first avenger name Thor
import Foundation

let hero = ClassHero(name: "Iron man", universe: "Marvel")

hero.name = "Cat Woman"
hero.universe = "DC"

Class๋Š” ์ƒ์ˆ˜๋กœ ์„ ์–ธ&์ƒ์„ฑํ–ˆ์–ด๋„ ์†์„ฑ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

Struct๋Š” ์ƒ์ˆ˜๋กœ ์„ ์–ธ&์ƒ์„ฑํ•˜๋ฉด ์†์„ฑ ๊ฐ’ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. Struct ๋Š” ๊ฐ’ ์œ ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

import Foundation

struct StructHero {
    
    var name: String
    var universe: String
    
    mutating func reverseName() -> String {
        self.name = String(self.name.reversed())
    }
}

Struct ๋‚ด์—์„œ ์†์„ฑ ๊ฐ’์„ ๋ฐ”๊พธ๋ ค๊ณ  ํ•˜๋ฉด Error ๊ฐ€ ๋‚œ๋‹ค. mutating ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋” ๊ฐ„๋‹จํ•˜๊ณ  ๋น ๋ฅด๊ณ  ๊ฐ’๋ณต์‚ฌ๋กœ ์ธํ•ด ์ถ”๋Œ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๊ธฐ์— struct ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ๋‚ซ๋‹ค.

๋‹ค๋งŒ struct ๋Š” ์ƒ์†์„ ๋ฐ›์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๊ณ  ์‹ถ์„ ๋• ์˜ณ์ง€ ์•Š๋‹ค.

๋”ฐ๋ผ์„œ Struct๋กœ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์ƒ์†์ด ํ•„์š”ํ•˜๊ฑฐ๋‚˜ Objective-C ๋กœ ์ž‘์—…ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋งŒ Calss ๋กœ ๋ฐ”๊พธ์ž.

 

* Access Levels

- private: { } ์•ˆ์—์„œ๋งŒ access ๊ฐ€๋Šฅํ•จ.

- fileprivate: ์†์„ฑ์ด๋‚˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์„ ์–ธ๋œ ํŒŒ์ผ ๋‚ด์—์„œ๋งŒ access ๊ฐ€๋Šฅํ•จ.

- internal: ๋ชจ๋“ˆ ๋‚ด์—์„œ ๋ชจ๋‘ access ๊ฐ€๋Šฅ. private or fileprivate ์„ ์„ ์–ธํ•˜์ง€ ์•Š์•˜์„ ๋•Œ defualt ๊ฐ’.

- public: ๋‹ค๋ฅธ ๋ชจ๋“ˆ๋ผ๋ฆฌ๋„ access ๊ฐ€๋Šฅ. ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ ๋ฐ ์„œ๋ธŒ ํด๋ž˜์‹ฑ ๋ถˆ๊ฐ€๋Šฅ.

- open: ๋‹ค๋ฅธ ๋ชจ๋“ˆ๋ผ๋ฆฌ๋„ access ๊ฐ€๋Šฅ. ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ ๋ฐ ์„œ๋ธŒ ํด๋ž˜์‹ฑ ๊ฐ€๋Šฅ.

 

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/accesscontrol/

 

Documentation

 

docs.swift.org

P.S) public, open ์ ‘๊ทผ์ž๋Š” ๋งŽ์ด ์‚ฌ์šฉ์•ˆํ• ๊ฑฐ ๊ฐ™์€๋ฐ framework, sdk ๊ฐœ๋ฐœ์‹œ์—” public, open ์ ‘๊ทผ ์ง€์ •์ž๊ฐ€ ์ค‘์š”ํ•˜๋‹ค.

 

* Tuples

: ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ.

import Foundation

let tuple1 = ("Angela", 12)
let tuple2 = (name: "Angela", age: 12)
let tuple3: (name: String, age: Int)

tuple3.name = "Angela"
tuple3.age = 18

tuple3 = (name: "Angela2", age: 21)

print(tuple1.0)
print(tuple2.name)

์„ ์–ธ์€ ์›ํ•˜๋Š” ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ์ดํ„ฐ๋ฅผ , ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋„ฃ์–ด์„œ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

์‚ฌ์šฉ์€ index ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค.

 

์„ ์–ธ ๋ฐ ํ˜ธ์ถœ ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋‹ค ์•Œ์•„๋‘์ž.

728x90