๋ฐ์ํ
ํด๋์ค VS ๊ตฌ์กฐ์ฒด
๊ณตํต์
- ๊ฐ์ ์ ์ฅํ๊ธฐ ์ํ ํ๋กํผํฐ ์ ์
- ๊ธฐ๋ฅ์ ์ ๊ณตํ๊ธฐ ์ํ ๋ฉ์๋ ์ ์
- subscript ๋ฌธ๋ฒ์ ์ด์ฉํด ํน์ ๊ฐ์ ์ ๊ทผํ ์ ์๋ subscript ์ ์
- ์ด๊ธฐ ์ํ๋ฅผ ์ค์ ํ ์ ์๋ initializer ์ ์
- ๊ธฐ๋ณธ ๊ตฌํ์์ ๊ธฐ๋ฅ ํ์ฅ
- ํน์ ํ ์ข ๋ฅ์ ํ์ค ๊ธฐ๋ฅ์ ์ ๊ณตํ๊ธฐ ์ํ ํ๋กํ ์ฝ ์์(conform)
์ฐจ์ด์ (ํด๋์ค๋ง ๊ฐ๋ฅ)
- ์์ (Inheritance): ํด๋์ค์ ์ฌ๋ฌ ์์ฑ์ ๋ค๋ฅธ ํด๋์ค์ ๋ฌผ๋ ค ์ค
- ํ์ ์บ์คํ (Type casting): ๋ฐํ์์ ํด๋์ค ์ธ์คํด์ค์ ํ์ ์ ํ์ธ
- ์๋ฉธ์ (Deinitializers): ํ ๋น๋ ์์์ ํด์ (free up) ์ํด
- ์ฐธ์กฐ ์นด์ดํธ (Reference counting): ํด๋์ค ์ธ์คํด์ค์ ํ๋ ์ด์์ ์ฐธ์กฐ๊ฐ ๊ฐ๋ฅ
๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
- ์ฃผ ๋ชฉ์ ์ด ๊ด๊ณ๋ ๊ฐ๋จํ ๊ฐ์ ์บก์ํ(encapsulate) ํ๊ธฐ ์ํ ๊ฒ์ธ ๊ฒฝ์ฐ
- ๊ตฌ์กฐ์ฒด์ ์ธ์คํด์ค๊ฐ ์ฐธ์กฐ๋๊ธฐ ๋ณด๋ค ๋ณต์ฌ๋๊ธฐ๋ฅผ ๊ธฐ๋ํ๋ ๊ฒฝ์ฐ
- ๊ตฌ์กฐ์ฒด์ ์ํด ์ ์ฅ๋ ์ด๋ ํ ํ๋กํผํฐ๊ฐ ์ฐธ์กฐ๋๊ธฐ ๋ณด๋ค ๋ณต์ฌ๋๊ธฐ๋ฅผ ๊ธฐ๋ํ๋ ๊ฒฝ์ฐ
- ๊ตฌ์กฐ์ฒด๊ฐ ํ๋กํผํฐ๋ ๋ฉ์๋ ๋ฑ์ ์์ํ ํ์๊ฐ ์๋ ๊ฒฝ์ฐ
ํด๋์ค
ํด๋์ค ์ ์ธ ๋ฌธ๋ฒ
class VideoMode {
var resolution = Resolution() // Resolution ๊ตฌ์กฐ์ฒด๋ฅผ ๊ฐ์ผ๋ก ์ฌ์ฉ
var interlaced = false
var frameRate = 0.0
var name: String?
}
ํด๋์ค ์ธ์คํด์ค ์์ฑ
ํด๋์ค ์ด๋ฆ ๋ค์ () ๋น ๊ดํธ ๋ถ์ด๋ฉด ์ธ์คํด์ค ์์ฑ
let someVideoMode = VideoMode() // ํด๋์ค ์ธ์คํด์ค ์์ฑ
ํ๋กํผํฐ ์ ๊ทผ
์ ๋ฌธ๋ฒ์ ์ฌ์ฉํด์ ํด๋์ค ์ธ์คํด์ค์ ํ๋กํผํฐ์ ํ์๋ ๋ฒจ ํ๋กํผํฐ์ ์ ๊ทผ ๊ฐ๋ฅ
print(\\(someVideoMode.frameRate)")
// 0.0 ์ถ๋ ฅ
์ ๋ฌธ๋ฒ์ ์ฌ์ฉํด์ ๊ฐ ํ ๋น ๊ฐ๋ฅ
someVideoMode.resolution.width = 1280
print("The width of someVideoMode is now \\(someVideoMode.resolution.width)")
// "The width of someVideoMode is now 1280" ์ด ์ถ๋ ฅ
ํด๋์ค๋ ์ฐธ์กฐ ํ์
โญ ์ฐธ์กฐ ํ์ ์ด๋?
๋ณ์๋ ์์์ ๊ฐ์ ํ ๋น์ ํ๊ฑฐ๋ ํจ์์ ์ธ์๋ก ์ ๋ฌํ ๋ ๊ทธ ๊ฐ์ด ๋ณต์ฌ๋์ง ์๊ณ ์ฐธ์กฐ(๊ทธ ๊ฐ์ ๊ฐ๊ณ ์๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ฐ๋ผ๋ณด๊ณ ์๋ค)๋จ
let tenEighty = VideoMode()
tenEighty.resolution = hd
tenEighty.interlaced = true
tenEighty.name = "1080i"
tenEighty.frameRate = 25.0
let alsoTenEighty = tenEighty
alsoTenEighty.frameRate = 30.0
print("The frameRate property of tenEighty is now \\(tenEighty.frameRate)")
// "The frameRate property of tenEighty is now 30.0" ์ถ๋ ฅ
- let alsoTenEighty = tenEighty ๊ตฌ๋ฌธ์์ alsoTenEighty์์๊ฐ tenEighty ์ธ์คํด์ค๋ฅผ ๋ณต์ฌํ ๊ฒ์ด ์๋๋ผ ์ฐธ์กฐํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋์ค์ 30์ผ๋ก ๋ณ๊ฒฝ๋ ๊ฐ์ผ๋ก ์ถ๋ ฅ๋จ
- ๋ฐ๋ผ๋ณด๊ณ ์๋ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๋์ผํ๊ฒ ๋ฐ๋ผ๋ณด๊ณ ์ฐธ์กฐํ๊ณ ์๋ ๊ฒ
์๋ณ ์ฐ์ฐ์
์์์ ๋ณ์๊ฐ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๊ณ ์๋์ง ๋น๊ตํ๊ธฐ ์ํด ์๋ณ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํจ
- === : ๋ ์์๋ ๋ณ์๊ฐ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๊ณ ์๋ ๊ฒฝ์ฐ ์ฐธ
- !== : ๋ ์์๋ ๋ณ์๊ฐ ๋ค๋ฅธ ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๊ณ ์๋ ๊ฒฝ์ฐ ์ฐธ
๊ตฌ์กฐ์ฒด
๊ตฌ์กฐ์ฒด ์ ์ธ ๋ฌธ๋ฒ
struct Resolution {
var width = 0
var height = 0
}
๊ตฌ์กฐ์ฒด
์ธ์คํด์ค ์์ฑ
๊ตฌ์กฐ์ฒด ์ด๋ฆ ๋ค์ () ๋น ๊ดํธ ๋ถ์ด๋ฉด ์ธ์คํด์ค ์์ฑ
let someResolution = Resolution() // ๊ตฌ์กฐ์ฒด ์ธ์คํด์ค ์์ฑ
ํ๋กํผํฐ ์ ๊ทผ
print("The width of someResolution is \\(someResolution.width)")
// "The width of someResolution is 0" ์ถ๋ ฅ
๊ตฌ์กฐ์ฒดํ์ ๋ฉค๋ฒ ์ด๊ธฐํ
- ๋ชจ๋ ๊ตฌ์กฐ์ฒด๋ ์ด๊ธฐํ ํ ๋ ํ๋กํผํฐ๋ฅผ ์ ์ธํ ์ ์๋ ์ด๊ธฐ์๋ฅผ ์๋ ์์ฑํด ์ ๊ณต
- ์๋ ์์์์ ๊ตฌ์กฐ์ฒด ์์ width์ heightํ๋กํผํฐ๋ง ์ ์ํ๋ค๋ฉด ์๋์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅ
let vga = Resolution(width: 640, height: 480)
๊ตฌ์กฐ์ฒด์ ์ด๊ฑฐํ์ ๊ฐ ํ์
โญ ๊ฐ ํ์ ์ด๋?
ํจ์์์ ์์๋ ๋ณ์์ ์ ๋ฌ๋ ๋ ๊ทธ ๊ฐ์ด ๋ณต์ฌ๋์ ์ ๋ฌ๋จ
let hd = Resolution(width: 1920, height: 1080)
var cinema = hd
๋ฐ์ํ
'โจ๏ธ Language > swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ๋ฉ์๋ (0) | 2024.03.21 |
---|---|
[Swift] ํ๋กํผํฐ (0) | 2024.03.20 |
[Swift] ํด๋ก์ (1) | 2024.03.18 |
[Swift] ์์ธ ์ฒ๋ฆฌ์ ํจ์ (4) | 2024.03.17 |
[Swift] ์กฐ๊ฑด/๋ฐ๋ณต๋ฌธ (1) | 2024.03.17 |