ํฐ์คํ ๋ฆฌ ๋ทฐ
[์ค์ํํธ(Swift) ํ๋ก๊ทธ๋๋ฐ] - ์ Swift์ ํด๋์ค(Class)์๋ ๋ฉค๋ฒ์์ด์ฆ ์ด๊ธฐํ(Memberwise Init)๊ฐ ์์๊น?
B_log 2021. 5. 18. 12:03๐ง ์ struct๋ง ์๋ ์์ฑ ์ด๊ธฐํ(Memberwise Init)๊ฐ ์กด์ฌํ๊ณ class์๋ ์ด ํธํ ๊ธฐ๋ฅ์ ๋ฃ์ด์ฃผ์ง ์์์๊น?
- struct๋ฅผ ์ฌ์ฉํ๋ค๋ณด๋ฉด memberwise init์ด๋ผ๋ ํธ๋ฆฌํ ๊ธฐ๋ฅ์ ์์ฃผ ํ์ฉํ๊ฒ ๋ฉ๋๋ค. ๊ทธ๋ฐ๋ฐ ์ด ํธ๋ฆฌํ ๊ธฐ๋ฅ์ ์ class๋ ๋ฃ์ด์ฃผ์ง ์์์ ํญ์ init์ ์์ฑํ๊ฒ ํ๋์ง์ ๋ํ ์๋ฌธ์ด ๋ค์์ต๋๋ค. ์ ์ ๋น์ทํ ์๋ฌธ์ ๊ฐ์ง ๋ถ๋ค์ด ์์ด์ ๊ทธ๋ถ๋ค์ ๊ธ์ ์ฐธ๊ณ ํด ์ค๋ช ํด๋ณด๊ฒ ์ต๋๋ค.
๐ ์ฐ์ ์๋์ ๊ธ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
The logic behind it could be because a struct is not subclassable, making it obvious that a complete initializer would be required, and as such it was made implicit by the language.
On the other hand, a
class
is subclassable, and as such you may want to design it so that it can only be initialized from subclasses, and as such an implicit initializer was not provided by the language.Ref: ์ถ์ฒ
(ํด์)struct๋ ์์์ด ๋ถ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ํด๋น struct์์ init์ ๊ผญ ํ์๋กํฉ๋๋ค. ๊ทธ๋์ Swift ์์ฒด์์ ์๋ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. ํ์ง๋ง class์ ๊ฒฝ์ฐ ์์์ด ๊ฐ๋ฅํ๋ฏ๋ก, ์ค๊ณ์ ํด๋น ํด๋์ค๊ฐ ์๋ธํด๋์ค์์๋ง ์ด๊ธฐํ๋์ผํ ํ์์ฑ์ด ์์ ์ ์์ต๋๋ค. ์ฆ, ์์ ๊ฐ๋ฅ์ฑ์์ ์ฐจ์ด๊ฐ ๋ฐ์ํฉ๋๋ค.
๐ ์ด๋ฅผ ์ข ๋ ๊ตฌ์ฒด์ ์ผ๋ก ์ค๋ช ํ ๊ธ์ด ์์ต๋๋ค.
...
The main reason is that classes have inheritance, which would make memberwise initializers difficult to work with. Think about it: if I built a class that you inherited from, then added some properties to my class later on, your code would break – all those places you relied on my memberwise initializer would suddenly no longer work.
So, Swift has a simple solution: rather than automatically generating a memberwise initializer, authors of classes must write their own initializer by hand. This way, you can add properties as much as you want without affecting the initializer for your class, and affecting others who inherit from your class. And when you do decide to change your initializer, you’re doing yourself, and are therefore fully aware of the implications for any inheriting classes.
Ref: ์ถ์ฒ
์ ๋ฆฌํด๋ณด๋ฉด ์์์ ์ธ๊ธํ๋๋ก class์ memberwise๊ฐ ์๋ ์ด์ ๋ ๊ฐ๋จํ ๋งํด์๋ '์์'์ด๊ณ , ์กฐ๊ธ ๋ ์ ํํ ์๊ธฐํ๋ฉด '์๋ํ๋ ์ด๊ธฐํ๊ฐ ์กด์ฌํ ๊ฒฝ์ฐ ์์์ ์ํฉ์์ ๋ฐ์ํ ์ ์๋ ์ค๋ฅ๋ฅผ ๊ฐ๋ฐ์๊ฐ ์ง์ ๋ณ๊ฒฝํ๋ ์์
์ด ๋ ๋ฒ๊ฑฐ๋กญ๊ธฐ ๋๋ฌธ์'๋ผ๊ณ ์ ์ํ ์ ์์ต๋๋ค.
๐ ์์๋ก ์ดํดํด๋ณด๊ธฐ
์์ ์ํฉ์ ์กฐ๊ธ๋ ์ง๊ด์ ์ผ๋ก ์ดํดํด๋ณด๊ธฐ ์ํด ์์๋ฅผ ๋ง๋ค์ด๋ณด๊ฒ ์ต๋๋ค.
class Car {
var name: String
init(name: String) {
self.name = name
}
}
class Lambo: Car {
var engine: String
init(engineName: String) {
self.engine = engineName
super.init(name: "Lambo")
}
}
let myCar = Lambo(engine: "V10")
print(myCar.name)
print(myCar.engine)
์์ ์์๋ ์ฝ๋์ ๋ฌธ์ ๋ ๊ฒ์ด ์๋ ์ฝ๋์ ๋๋ค. ์๋์ฐจ๋ผ๋ ๊ฐ์ฒด๊ฐ ์กด์ฌํ๊ณ , ์ด๊ฒ์ ์์๋ฐ์ Lambo๋ผ๋ ๊ฐ์ฒด๊ฐ ์์ฑ๋ฉ๋๋ค. ์ด๊ฒ์ ๊ท๋ฅ๋ฒ(?)์ผ๋ก ์๊ฐํ์ฌ ๋ง์ฝ class์ memberwise init ๊ธฐ๋ฅ์ด ์กด์ฌํ๋ค๋ฉด? ์ด๋ผ๋ ์๊ฐ์ ํด๋ณด๊ฒ ์ต๋๋ค.
์ฐ์ ํด๋์ค Car๋ init์ด ๋ช
์๋์ง ์์๋ ๋๊ธฐ ๋๋ฌธ์ name์ด๋ผ๋ ํ๋กํผํฐ๋ง ๊ฐ์ ธ๋ ์๊ด์ด ์์ต๋๋ค. Memberwise Init์ด name์ ์ ์ํด์ค ๊ฒ์ด๊ธฐ ๋๋ฌธ์ด์ฃ ! ๊ทธ๋ฆฌ๊ณ ์๋ Lambo์์๋ ๋ง์ฐฌ๊ฐ์ง๋ก Car์ ์ด๋ฆ์ผ๋ก Lambo๋ฅผ ์ ์ํ ๊ฒ์
๋๋ค. ๋ง์ฝ ์ด ์ํฉ์์ Car์ modelName์ด๋ผ๋ ํ๋กํผํฐ๊ฐ ์ถ๊ฐ๋๋ฉด ์ด๋ป๊ฒ ๋ ๊น์? Lambo๋ ์ฆ์ ์๋ฌ๋ฅผ ๋ฐ์์ํฌ ๊ฒ์
๋๋ค. ๋ฌผ๋ก , ์์ ์์์ฒ๋ผ ๊ฐ๋จํ ๊ตฌ์กฐ์ ๊ฒฝ์ฐ ์ฆ์ ์์ ๊ฐ๋ฅํ ์ ์์ต๋๋ค. ํ์ง๋ง ์์ ๊ตฌ์กฐ๊ฐ 100๊ฐ๊ฐ ์๊ธฐ๊ณ , ์ถ๊ฐ์ ์ผ๋ก ๋งค์ฐ ๋ณต์กํ ์ํคํ
์ณ๋ก ๊ตฌ์ฑ๋์ด์๋ค๋ฉด ์ด๋จ๊น์? ์์ ์์
์์ฒด๊ฐ ๋งค์ฐ ํ๋ค์ด์ง ์ ์์ต๋๋ค. ์๊ทธ๋๋ ํ๋ ์์ ์์
์, ๋ฌต์์ ์ผ๋ก ์ ์ธ ๋์ด์๋ init์ ์์ ํ๋ ๊ฒ์ ํจ์ฌ ๋ ๋ฒ๊ฑฐ๋ก์ด ์์
์ด ๋ ์ ์์ต๋๋ค.
๐ ์ต์ข ์ ๋ฆฌ
class์์ Memberwise Init ๊ธฐ๋ฅ์ ๊ตฌํํด์ฃผ์๋ค๋ฉด ๊ธฐ๋ฅ์์ ํฐ ๋ฌธ์ ๋ฅผ ์ผ์ผํฌ๊น์? ์ด๊ฒ์ ๋ํ ๋๋ต์ "ํด๊ฒฐํ ์ ์๋ ๋ฌธ์ ์ผ ๊ฐ๋ฅ์ฑ์ด ํฌ๋ค"๋ผ๊ณ ํ ์ ์์ต๋๋ค. ๊ทธ๋ผ ์ ๊ธฐ๋ฅ์ด ์๋ ๊ฑธ๊น์? ์ฌ๊ธฐ์ ๋ํ ๋๋ต์ "์๋ํ๋ ๊ธฐ๋ฅ์ด ๋ฌธ์ ๋ฅผ ์ผ์ผํฌ ์ฌ์ง๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ง์ ๊ตฌํํ๊ฒ ๋ง๋๋ ๊ฒ์ด ์คํ๋ ค ์์
์ ํธํ๊ฒ ๋ง๋ค์ด ์ค ์ ์๋ค" ๋ผ๊ณ ์ ๋ฆฌํ ์ ์์ต๋๋ค.
'iOS ์ฑ๊ฐ๋ฐ > Swift Basic' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- Total
- Today
- Yesterday
- ๋ฐฑ์ค ์๊ณ ๋ฆฌ์ฆ#BackTracking
- ์ํธ์ฝ๋#dp#BOJ#Python
- ํ์ด์ฌ์๊ณ ๋ฆฌ์ฆ์ธํฐ๋ทฐ#4์ฅ
- ๋ฏธ๋ก ํ์#๋ฐฑ์ค์๊ณ ๋ฆฌ์ฆ#Python
- N์ผ๋ก ํํ#DP#Programmers#Python
- ์์ด์ฌ์ดํด#BOJ#Python
- ๋๋ฌด์๋ฅด๊ธฐ#BOJ#์ด๋ถํ์#Python
- django#slicing
- Distinct#Codility#Python
- ์ฌ์๊ฐ์#๋ฐฑ์ค์๊ณ ๋ฆฌ์ฆ#Python
- ์ฟผ๋ํธ๋ฆฌ#BOJ#๋ถํ ์ ๋ณต#Python
- django
- Swift#Tuples#Range
- ๋์ ์๋ฅด๊ธฐ#์ด๋ถํ์#BOJ#Python
- filter#isalnum#lower
- ์ข ์ด์๋ฅด๊ธฐ#๋ถํ ์ ๋ณต#BOJ#Python
- ํ ํ๋ก์ ํธ#๋ฐฑ์ค์๊ณ ๋ฆฌ์ฆ#Python
- API#lazy#
- ๋ฆฌ๋ชจ์ปจ#์์ ํ์#BOJ#Python
- PassingCars#Codility#Python
- ๋ฐ๋ณต์์ด#๋ฐฑ์ค์๊ณ ๋ฆฌ์ฆ#Python
- ํฐํ๋น์น#๋ฆฌ์ฝ#xbox#controller
- NumberofDiscIntersections#Codility#Sort#Python
- ๋ฐฐ์ดํฉ์น๊ธฐ#๋ถํ ์ ๋ณต#BOJ#Python
- ํ ๋งํ #๋ฐฑ์ค์๊ณ ๋ฆฌ์ฆ#Python
- ๋ ์ง ๊ณ์ฐ#BOJ#์์ ํ์#Python
- ๋ณ๋ ๋์ดํธ#BOJ#ํ์๋ฒ#Python
- ๊ณต์ ๊ธฐ ์ค์น#BOJ#์ด๋ถํ์#Python
- Triangle#Sorting#Codility#Python
- Brackets#Stacks and Queues#Codility#Python
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |