๋๋ ์ง์นจ์ ๋ช ์๋ ๋๋ถ๋ถ์ ๋ด์ฉ์ ๋์ํ์ง๋ง ์ด๊ฒ์ ์ผ๊ด์ฑ์ด ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
https://github.com/raywenderlich/swift-style-guide#type -annotation-for-empty-arrays-and-dictionaries
๊ฐ์ด๋๋ ๋ค์์ ๋ํ๋ ๋๋ค.
// preferred
var names: [String] = []
var lookup: [String: Int] = [:]
๋ณด๋ค ์ ํธ๋๋ค
// not preferred
var names = [String]()
var lookup = [String: Int]()
๊ทธ๋ฌ๋ ์ด ๊ฐ์ด๋๋ ์ผ๋ฐ์ ์ผ๋ก ๊ฐ๊ฒฐํ ์ฝ๋๋ฅผ ์ํด ์ถ๋ก ๋ ์ ํ์ ์ ํธํฉ๋๋ค.
// preferred
let message = "Click the button"
let currentBounds = computeViewBounds()
```๋น ๋ฅธ
// ์ ํธํ์ง ์์
let message: String = "๋ฒํผ์ ํด๋ฆญํ์ธ์"
currentBounds: CGRect = computeViewBounds()
If we were being consistent, shouldn't we always prefer inferred types over explicit declaration? I would think this is more consistent
```swift
// should be preferred
var names = [String]();
var lookup = [String: Int]();
์ ์๊ฐ์๋ ์ฌ๊ธฐ ๊ฐ์ด๋๊ฐ ๋ง์ต๋๋ค. ํ์ํ ๋ ์ ํ์ ์ง์ ํ๋ฉด ๋ฉ๋๋ค. ์์ ๋๋ ๋ฒ๋ฆฌ์ญ์์ค.
์ข์, ํ์ง๋ง ์ด๊ฒ์ ์ ๋ง ์คํ์ผ์ ์ ํ์ด ์๋๊ฐ? ์๋ ์ ๋ง. ๋ช ๊ฐ์ง ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
// preferred
var colors: [UIColor]?
var colors: [UIColor] = []
var colors: [UIColor] = [.red, .green, .blue]
์ด์ ์ด 3๊ฐ์ง ์๋ฅผ ์ ์๋ ๊ธฐ๋ณธ ์ค์ ์ผ๋ก ๋ณํํด ๋ณด๊ฒ ์ต๋๋ค.
var colors: [UIColor]?
var colors = [UIColor]()
var colors = [UIColor.red, UIColor.green, UIColor.blue]
๊ทธ๊ณณ์์ ๋ฌด์จ ์ผ์ด ์ผ์ด๋ฌ๋์ง ๋ณผ๊น์? ๋ง์ง๋ง ํ์ ๊ฒฝ์ฐ ์ด์ ๋ฐฐ์ด์ ๋ชจ๋ ์์์ ๋ํด UIColor๋ฅผ ๋ฐ๋ณตํด์ผ ํฉ๋๋ค. ๋ํ ์ ์ธ์ด ์๋ก ๋ ์ผ๊ด์ฑ์ด ์๋ค๊ณ ์ฃผ์ฅํ ์๋ ์์ต๋๋ค.
๊ทธ๋ผ ๋ฐฉ๋ฒ์? ๋ฌผ๋ก ์ผ๊ด์ฑ์ ์ํด ํญ์ ์ ํ์ ์ง์ ํ๋๋ก ์ ํํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ๋๋ถ๋ถ์ ๊ฐ๋ฐ์๋ ์ค๋ณต ์ฝ๋๋ฅผ ์์ ๊ณ ์ถ์ดํ ๊ฒ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค.
// do we need to specify type?
var colors: [UIColor] = rainbowColors()
// no we don't
var colors = rainbowColors()
๋ง์ด ๋ผ?
_ํธ์ง: ๊ณต์ ํ๊ฒ ๋งํ๋ฉด ์ด์ ๊ฐ์ ๋ฐฐ์ด์ ์ ์ถํ ์๋ ์์ง๋ง ์ผ๊ด์ฑ์ ๋ํด ์ด์ผ๊ธฐํ๊ณ ์๋ค๋ ์ ์ ๊ฐ์ํ ๋ ์ด ๋ฐฐ์ด์ ํ ์ด๋ธ์์ ์ ์ธํ๋ ๊ฒ์ผ๋ก ์๊ฐํ๊ณ ์์ต๋๋ค. ๊ทธ๋ฅ ๋ด๊ฐ ์ณ์ ์ค ์์๋๋ฐ._
// preferred
var colors: [UIColor] = [.red, .green, .blue]
// not preferred
var colors = [.red, UIColor.green, .blue]
๋๋ ํจ๊ป ๊ฐ์ ๊ฒ์ด๋ค
var colors: [UIColor]?
var colors = [UIColor]()
var colors: [UIColor] = [.red, .green, .blue]
์์์ ํ ๊ฒ ๋์ . ๋ช ์์ ์ ์ธ์ด ํ์ํ ๊ฒฝ์ฐ๋ฅผ ์ ์ธํ๊ณ ์ ์ถ๋ ์ ํ์ ์ฌ์ฉํ๋ ๊ฒ์ ์ ํธํ์ง ์์ต๋๊น? ๊ทธ๋ฐ ์๋ฏธ์์ ์ธ ๋ฒ์งธ ์ค์ ์ผ๊ด์ฑ์ด ์์ด ๋ณด์ ๋๋ค.
๋ด๊ฐ ์๋ง๋ ๊ท์น์ ์ง๋์น๊ฒ ๋จ์ํํ๋ค๋ ์๋ฏธ์์ ๋น์ ์ด ์ณ์ต๋๋ค. ์ฆ, ๋ ์ ๊ทผ ๋ฐฉ์ ๋ชจ๋ ์๋ชป๋ ๊ฒ์ ์์ผ๋ฉฐ ์ ํธ๋์ ๋ฐ๋ผ ๋ค๋ฆ ๋๋ค. ์ ๊ฒฝ์ฐ์๋ ๋น์ด ์๊ฑฐ๋ ๋น์ด ์์ง ์์ ์ํ๋ฅผ ์๋ค๊ฐ๋ค ํ ๋ ์๋๊ฐ ๋ ์ผ๊ด๋๊ณ ์ฝ๊ธฐ ์ฝ๊ณ ์ค์ฉ์ ์ด๋ผ๋ ๊ฒ์ ์์์ต๋๋ค. ๋ค๋ฅธ ์ฌ๋๋ค์ด ๋ผ์ด๋ค๋๋ก ํด์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
// preferred
var colors: [UIColor]?
var colors: [UIColor] = []
var colors: [UIColor] = [.red]
// not preferred (in my opinion)
var colors: [UIColor]?
var colors = [UIColor]()
var colors: [UIColor] = [.red]
๋ฌธ์ ๋ ๋ค์๊ณผ ๊ฐ์ ๋ถ์ผ์น์ ๋๋ค.
// inconsistent?
var colors: [UIColor] = []
var colors = rainbowColors()
// your preference
var colors = [UIColor]()
var colors = rainbowColors()
@RobertGummesson , ๋ฐฐ์ด/์ฌ์ ๋ฆฌํฐ๋ด ๊ตฌ๋ฌธ๊ณผ ํจ๊ป ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ ์ ํ๋ง ๊ณ ๋ คํ๊ณ ์์ต๋๋ค. ๋ฐฐ์ด ๋ฆฌํฐ๋ด๋ก ์ฑ์ธ ๋ฐฐ์ด๋ง ์๋ค๋ฉด ๋ ์๋ฃจ์ ๋ชจ๋ ๊ด์ฐฎ์ ๊ฒ์ ๋๋ค. ๋๋ ๋น์ ์ด ์ ์ํ ๊ฒ์ ์ฌ์ฉํ๊ธฐ๋ํ์ต๋๋ค. ๊ทธ๋ฌ๋ ์ฐ๋ฆฌ๋ ์ธํธ๋ฅผ ์ป์๊ณ ๊ทธ๊ฒ๋ค๋ ํฌํจํ๋ ์ปจ๋ฒค์ ์์ ํ์คํํด์ผ ํ์ต๋๋ค. ๋น์ ์ด ์ ์ํ๋ ๊ฒ์ ๋ช ์์ ์ธ ํ์ดํ๋ณด๋ค ์ถํฉ๋๋ค.
var ints: Set = [1, 1, 2, 2]
var ints = Set([1, 1, 2, 2])
var ints = Set(arrayLiteral: 1, 1, 2, 2)
๋ฐ๋ผ์ ๋น์ด ์์ง ์์ ์ธํธ์ ๋ ๋์ ์ต์์ ์ ํ์ ๊ฒฐ์ ํ ํ ๋น ๋ฒ์ ๊ณผ ์ผ์นํ๋ ๋ฐฐ์ด ๊ท์น์ ๋ฐฑํฌ๋ฐํ ์ ์์ต๋๋ค.
var ints: Set<Int> = []
var ints = Set<Int>() // instead of this, which, without the above, seems fine.
var ints = [1, 1, 2, 2]
var ints: [Int] = []
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ ์๊ฐ์๋ ์ฌ๊ธฐ ๊ฐ์ด๋๊ฐ ๋ง์ต๋๋ค. ํ์ํ ๋ ์ ํ์ ์ง์ ํ๋ฉด ๋ฉ๋๋ค. ์์ ๋๋ ๋ฒ๋ฆฌ์ญ์์ค.
์ข์, ํ์ง๋ง ์ด๊ฒ์ ์ ๋ง ์คํ์ผ์ ์ ํ์ด ์๋๊ฐ? ์๋ ์ ๋ง. ๋ช ๊ฐ์ง ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
์ด์ ์ด 3๊ฐ์ง ์๋ฅผ ์ ์๋ ๊ธฐ๋ณธ ์ค์ ์ผ๋ก ๋ณํํด ๋ณด๊ฒ ์ต๋๋ค.
๊ทธ๊ณณ์์ ๋ฌด์จ ์ผ์ด ์ผ์ด๋ฌ๋์ง ๋ณผ๊น์? ๋ง์ง๋ง ํ์ ๊ฒฝ์ฐ ์ด์ ๋ฐฐ์ด์ ๋ชจ๋ ์์์ ๋ํด UIColor๋ฅผ ๋ฐ๋ณตํด์ผ ํฉ๋๋ค. ๋ํ ์ ์ธ์ด ์๋ก ๋ ์ผ๊ด์ฑ์ด ์๋ค๊ณ ์ฃผ์ฅํ ์๋ ์์ต๋๋ค.
๊ทธ๋ผ ๋ฐฉ๋ฒ์? ๋ฌผ๋ก ์ผ๊ด์ฑ์ ์ํด ํญ์ ์ ํ์ ์ง์ ํ๋๋ก ์ ํํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ๋๋ถ๋ถ์ ๊ฐ๋ฐ์๋ ์ค๋ณต ์ฝ๋๋ฅผ ์์ ๊ณ ์ถ์ดํ ๊ฒ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค.
๋ง์ด ๋ผ?
_ํธ์ง: ๊ณต์ ํ๊ฒ ๋งํ๋ฉด ์ด์ ๊ฐ์ ๋ฐฐ์ด์ ์ ์ถํ ์๋ ์์ง๋ง ์ผ๊ด์ฑ์ ๋ํด ์ด์ผ๊ธฐํ๊ณ ์๋ค๋ ์ ์ ๊ฐ์ํ ๋ ์ด ๋ฐฐ์ด์ ํ ์ด๋ธ์์ ์ ์ธํ๋ ๊ฒ์ผ๋ก ์๊ฐํ๊ณ ์์ต๋๋ค. ๊ทธ๋ฅ ๋ด๊ฐ ์ณ์ ์ค ์์๋๋ฐ._