๐คจ UI ์ปดํฌ๋ํธ ์ธ์คํด์ค๋ฅผ ํด๋ก์ ๋ก ์์ฑํ๋ ์ด์ ๊ฐ ๋ญ๊น?
์ด ๊ธ์ ์ ๊ถ๊ธ์ฆ์ผ๋ก ์์๋์์ต๋๋ค.
์ฑ์ ๊ฐ๋ฐํ ๋ UI์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ViewController์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๊ณผ์ ์ด ํ์ํ์ต๋๋ค.
class ViewController: UIViewController {
// ์ธ์คํด์ค ์์ฑ ํ ๋ฉ์๋๋ฅผ ํตํด ํด๋น ์ธ์คํด์ค์ ์์ฑ ์ด๊ธฐํ
let label1 = UILabel()
func configureUI() {
label1.text = "Hello, World!"
label1.textColor = .black
label1.textAlignment = .center
label1.font = .boldSystemFont(ofSize: 20)
}
// ์ธ์คํด์ค ์์ฑ๊ณผ ํจ๊ป ํด๋น ์ธ์คํด์ค์ ์์ฑ ์ด๊ธฐํ
let label2: UILabel = {
let label = UILabel()
label.text = "Hello, World!"
label.textColor = .black
label.textAlignment = .center
label.font = .boldSystemFont(ofSize: 20)
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
configureUI() // ์์ฑ ์ด๊ธฐํ
}
}
์ ๊ฐ ์ฌ์ฉํ๋ ์ปดํฌ๋ํธ๋ฅผ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ์ ์ ์ฝ๋์ฒ๋ผ ๋๊ฐ์ง๊ฐ ์์ต๋๋ค.
1. ์ธ์คํด์ค ์์ฑ ํ ๋ฉ์๋๋ฅผ ํตํด ํด๋น ์ธ์คํด์ค์ ์์ฑ ์ด๊ธฐํ
2. ์ธ์คํด์ค ์์ฑ๊ณผ ํจ๊ป ํด๋น ์ธ์คํด์ค์ ์์ฑ ์ด๊ธฐํ
๋ ๊ฐ์ ์ธ์คํด์ค๋ ํด๋น ์ธ์คํด์ค์ ์ด๊ธฐํ ์์ ๋ง ๋ค๋ฅผ๋ฟ ํฐ ์ฐจ์ด๋ ์์ด๋ณด์ ๋๋ค.
๋ค๋ง ์ฌ๋ฌ๊ฐ์ง์ ์๋ฌธ์ด ๋ค๊ฒ๋๋๋ฐ์ ํ๋ฒ ์ ๋ฆฌํด๋ด ์๋ค
๐ง ์ธ์คํด์ค ์์ฑ ํ ์์ฑ์ ์ด๊ธฐํ ํ ๋ ๊ผญ ๋ฉ์๋ ์์์๋ง ํ ๋นํด์ผํ ๊น?
์ฒซ๋ฒ์งธ ์ด๊ธฐํ๋ฐฉ๋ฒ์์ ์ configureUI() ๋ฉ์๋ ์์์ ์์ฑ์ ํ ๋นํด์ผ๋ง ํ ๊น์?
์๋์ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ์ปดํ์ผ๋ฌ๊ฐ ์ค๋ฅ๋ฅผ ๋ฐ์์ํต๋๋ค.
class ViewController: UIViewController {
let label = UILabel()
label.text = "Hello, World!" // ์ปดํ์ผ ์๋ฌ
label.textColor = .black
label.textAlignment = .center
label.font = .boldSystemFont(ofSize: 20)
override func viewDidLoad() {
super.viewDidLoad()
}
}
์ค๋ฅ๋ฅผ ๋ฒ์ญํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
โ๏ธ 1. Consecutive declarations on a line must be separated by ';'
- ํ ์ค์ ์ฐ์๋ ์ ์ธ์ ';' ๋ก ๊ตฌ๋ถํด์ผ ํฉ๋๋ค.
โ๏ธ 2. Expected '(' in argument list of function declaration
- ํจ์ ์ ์ธ์ ์ธ์ ๋ชฉ๋ก์ '(' ๊ฐ ํ์ํฉ๋๋ค.
โ๏ธ 3. Expected 'func' keyword in instance method declaration
- ์ธ์คํด์ค ๋ฉ์๋ ์ ์ธ์ 'func' ํค์๋๊ฐ ํ์ํฉ๋๋ค.
โ๏ธ 4.Expected declaration / Invalid redeclaration of 'label()'
- ์์ ์ ์ธ / 'lable()'์ ์๋ชป๋ ์ฌ์ ์ธ
fix๋ฒํผ์ ๋๋ฌ๋ ์๋ฌ๋ ๊ณ ์ณ์ง์ง ์์๋๋ฐ์.
์ค๋ฅ๋ค์ ์ข ํฉํด๋ณด๋ ์ปดํ์ผ๋ฌ๋ ํด๋น ์ธ์คํด์ค์ ์์ฑ์ ๋ณ๊ฒฝํ๋ ๊ฒ์ด ์๋ชป๋ ๋ฒ์์์ ์ด๋ฃจ์ด์ก๋ค๊ณ ๋ณด์ ๋๋ค.
์ด๋ Class์์์ ํ๋กํผํฐ๋ฅผ ์ง์ ์คํํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
Swift์์ ํด๋์ค๋ ํ๋์ ์ฒญ์ฌ์ง(blueprint)
Swift๋ฟ๋ง ์๋๋ผ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์์ ํด๋์ค๋ ์คํ์ฝ๋๊ฐ ์๋ ํ๋์ ์ค๊ณ๋๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
ํด๋์ค๋ง ์์ฑํด์๋ ์คํํ ์ ์๊ณ ์ธ์คํด์ค๋ฅผ ์์ฑํด์ผ๋ง์ด ํด๋์ค๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ์ค๊ณ ๋ฉ์๋๋ฅผ ํธ์ถํด์ผ ์คํ์ด ๋ฉ๋๋ค.
๊ทธ๋์ ์ค๊ณ๋ ๋ด๋ถ์์๋ ํ๋กํผํฐ, ๋ฉ์๋, ์์ฑ์ ๋ฑ "ํด๋์ค ๊ตฌ์ฑ์์์ ์ ์"๋ง ๊ตฌํํ ์ ์์ต๋๋ค.
๐ง ํด๋ก์ ๋ ์ต๋ช ํจ์์ธ๋ฐ ์ด๊ธฐํํ ๋ ์คํ์ํจ๊ฑฐ ์๋๊ฐ์?
let label: UILabel = {
let label = UILabel()
label.text = "Hello, World!"
label.textColor = .black
label.textAlignment = .center
label.font = .boldSystemFont(ofSize: 20)
return label
}()
์ ํจ์๋ฅผ ๋ฏ์ด๋ณด๋ฉด label ํ๋กํผํฐ์ ํ์ ์ UILabel์ด๊ณ , ์ด๋ ํด๋ก์ ๋ด๋ถ์์ ๋ฆฌํด๋ label์ผ๋ก ์ด๊ธฐํ๋ฉ๋๋ค.
์๊ดํธ ()๊ฐ ์๋ค๋ฉด ํด๋ก์ ๋ง ๊ตฌํํ ๊ฒ์ด๊ณ ์ ์ฝ๋๋ ํด๋ก์ ์์ฑ๊ณผ ํจ๊ป ์คํ์ํค๊ณ ์์ต๋๋ค.
ํด๋ก์ ๋ฅผ ๋ถ๋ฆฌํ์๋ฉด ์๋ ์ฝ๋์ ๋น์ทํฉ๋๋ค.
let configureLabel: () -> UILabel = {
let label = UILabel()
label.text = "Hello, World!"
label.textColor = .black
label.textAlignment = .center
label.font = .boldSystemFont(ofSize: 20)
return label
}
let label: UILabel = configureLabel()
ํด๋ก์ ์ ํจ๊ป ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ฉด ํด๋ก์ ๊ตฌํ ๋ฐ ์คํ๋ถ๋ถ์ ํด๋์ค์ ๋ฒ์๊ฐ ์๋๋ผ ์ธ์คํด์ค ์์ฑ์ ๋ฒ์์ด๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ๋ฌ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์ต๋๋ค.
๐ง ํด๋ก์ ๋ ๋ฉ์๋๋ ๊ตฌํ๋ง ํด๋๊ณ ์ธ์คํด์ค ๋ง๋ค๋ ์ฌ์ฉํ๋ฉด ๋์ง ์๋์?
class ViewController: UIViewController {
let configureLabelClosure: () -> UILabel = {
let label = UILabel()
label.text = "Hello, World!"
label.textColor = .black
label.textAlignment = .center
label.font = .boldSystemFont(ofSize: 20)
return label
}
func configureLabelMethod() -> UILabel {
let label = UILabel()
label.text = "Hello, World!"
label.textColor = .black
label.textAlignment = .center
label.font = .boldSystemFont(ofSize: 20)
return label
}
// ์ปดํ์ผ ์๋ฌ โ๏ธ
let label1: UILabel = configureLabelMethod()
let label2: UILabel = configureLabelClosure()
override func viewDidLoad() {
super.viewDidLoad()
}
}
์ ์ฝ๋๋ UILabel์ ๋ฆฌํดํ๋ ๋๊ฐ์ ํด๋ก์ ์ ๋ฉ์๋๊ฐ ๊ตฌํ๋์ด์์ต๋๋ค.
๊ตฌํ๋ถ์ ์์ฑ๋ถ๋ถ์ ๋๋์ด ๋ณด์๋๋ฐ ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค.
โ๏ธ Cannot use instance member 'configureLabelMethod' within property initializer; property initializers run before 'self' is available
- ์์ฑ ์ด๊ธฐํ ํ๋ก๊ทธ๋จ ๋ด์์ ์ธ์คํด์ค ๋ฉค๋ฒ 'configureLabelMethod'๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์์ฑ ์ด๊ธฐํ ํ๋ก๊ทธ๋จ์ 'self'๋ฅผ ์ฌ์ฉํ ์ ์๊ธฐ ์ ์ ์คํ๋ฉ๋๋ค.
์ด ๋ป์ ViewControllerํด๋์ค์ ๋ฉ์๋์ธ configureLabelMethod๋ ViewController๊ฐ ์ด๊ธฐํ๋ ํ์ ์ฌ์ฉํ ์ ์์ง๋ง, ํด๋น ์ด๊ธฐํ ์์ ์ ViewController์ ์ด๊ธฐํ๊ฐ ์๋ฃ๋์ง ์์๊ธฐ ๋๋ฌธ์ ํด๋น ๋ฉ์๋๋ฅผ ์คํํ ์ ์๋ค๋ ๋ป์ ๋๋ค.
๊ทธ๋ ๊ธฐ์ ViewController์ ์ด๊ธฐํ๊ฐ ์๋ฃ๋๋ฉด ํด๋น ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๊ทธ๋ ๋ค๋ฉด ์์ ์คํํ ์ ์๋ ๊ตฌ์กฐ์ผ๊น์?
์ ์ฝ๋์ฒ๋ผ ํด๋์ค๋ฅผ ๋ฆฌํดํ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ํด๋์ค๊ฐ ๋ชจ๋ ์ด๊ธฐํ ๋ ํ ์คํ๋๋๋ก ๋ช ์ํ๋ lazyํค์๋์ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค.
class ViewController: UIViewController {
let configureLabelClosure: () -> UILabel = {
let label = UILabel()
label.text = "Hello, World!"
label.textColor = .black
label.textAlignment = .center
label.font = .boldSystemFont(ofSize: 20)
return label
}
func configureLabelMethod() -> UILabel {
let label = UILabel()
label.text = "Hello, World!"
label.textColor = .black
label.textAlignment = .center
label.font = .boldSystemFont(ofSize: 20)
return label
}
// ์ ์์ ์ผ๋ก ์๋
lazy var label1: UILabel = configureLabelMethod()
lazy var label2: UILabel = configureLabelClosure()
override func viewDidLoad() {
super.viewDidLoad()
}
}
์ถ๊ฐ์ ์ผ๋ก...
๐ง .init() ์ผ๋ก๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋๋ฐ์?
์ ๋๊ฐ์ง ๊ฒฝ์ฐ ๋ง๊ณ ๋ .init() ์ผ๋ก๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
class ViewController: UIViewController {
let label: UILabel = .init()
let customView: UIView = .init()
override func viewDidLoad() {
super.viewDidLoad()
}
}
์ด๋ label ํ์ ์ด UILabel์ผ๋ก ์ถ๋ก ๋๋ฏ๋ก UILabel.init()์ด ์คํ๋ ์ ์์ต๋๋ค.
๋ํ ์ถ๋ก ๋๋ค๋ฉด init ํธ์ถ ์์ ํด๋น ํ์ ์ด ์๋ต๋ ์ ์์ต๋๋ค.
// label ํ์
์ด UILabel๋ก ๋ช
์๋์ด ์์ผ๋ฏ๋ก ํด๋น ํด๋์ค์ init์ ์คํํ ์ ์๋ค.
let label: UILabel = .init()
let label: UILabel = UILabel.init()
// โ๏ธ ํด๋น ์ฝ๋๋ ํ์
์ ์ ์ ์์ผ๋ฏ๋ก init์ ์์ฑํ ์ ์๋ค.
let label = .init()
UILabel.init()์ ๋ง์น ์ ์ ๋ฉ์๋๋ฅผ ์คํํ๋๊ฒ๊ณผ ๋น์ทํด๋ณด์ ๋๋ค.
struct Person {
let name: String
let age: Int
static func configurePerson() -> Person {
return Person(name: "Cheolsun", age: 7)
}
}
// ๋์ผํ๊ฒ ์๋
let person: Person = Person.configurePerson()
let person: Person = .configurePerson()
๐ง ์์ฑ์๋ ์ ์ (static) ํจ์์ธ๊ฐ์?
์ ๋ต์ NO์ ๋๋ค.
์์ฑ์๋ ์ธ์คํด์ค๋ฅผ ๋ง๋๋ ํน์ํ ๋ฉค๋ฒํจ์์ด์ง๋ง ์ ์ ํจ์์ฒ๋ผ ๋น์ทํ๊ฒ ํธ์ถ๋ฉ๋๋ค.
๋ค์ ๋ณธ๋ก ์ผ๋ก ๋์์
๐คจ UI ์ปดํฌ๋ํธ ์ธ์คํด์ค๋ฅผ ํด๋ก์ ๋ก ์์ฑํ๋ ์ด์ ๊ฐ ๋ญ๊น?
ํด๋ก์ ๋ก ์์ฑํ๋ ๋ฐฉ๋ฒ์ด๋, ๋ฉ์๋๋ ์์ฑ์๋ฅผ ํตํด ์์ฑํ๋ ๋ฐฉ๋ฒ์๋ ๊ตฌํ๋ฐฉ์์ ์ฐจ์ด๋ ์์ง๋ง ์ธ์คํด์ค์ ์ฌ์ฉ๋ฐฉ์์๋ฐ๋ผ ์ ํํ๋ ๋ฐฉ์์ด ๋ค๋ฅผ ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
ํด๋ก์ ๋ก ์์ฑํ๋ค๋ฉด ์ด๊ธฐํ ๋ฉ์๋๋ฅผ ๊ตฌํํ์ง ์์๋ ์ธ์คํด์ค ์์ฑ๊ณผ ํจ๊ป ์์ฑ์ด ์ด๊ธฐํ๋๋ ์ฅ์ ์ด ์์ง๋ง
์์ฑ์ด ์์ฃผ ๋ณํ๋ค๋ฉด ์ด๊ธฐํ ๋ฉ์๋๋ฅผ ๊ตฌํํ์ฌ ๋ณํ๋ ์์ ์ ํธ์ถํ๋๋ก ๊ตฌํํ๋ฉด ๋ ์ข์ ๋ฐฉ๋ฒ์ด ๋ ์ ์์ต๋๋ค!
'Swift > ๋ฌธ๋ฒ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Swift์ ํ์ ๋ค Types (0) | 2025.03.26 |
---|---|
ARC dive deep (0) | 2025.03.21 |
ํํ ํด๋ก์ (0) | 2025.03.21 |
enum์ ๋ค๋ฅธ ์ฌ์ฉ๋ฐฉ๋ฒ (0) | 2025.03.14 |
Swift Protocols (0) | 2025.03.11 |