github StyleShare κ°μ΄λλ₯Ό κΈ°μ€μΌλ‘ μμ± λ¨.
μ΅μ’ μμ μΌ : 2022-05-18
- Swift Style Guide
- λͺ©μ°¨
- νν©
- StyleGuideμ© λΌμ΄λΈλ¬λ¦¬
- Style Guide
- νλ‘κ·Έλλ° κΆμ₯μ¬ν
- SwiftLint μ¬μ© κ°μ΄λ
-
- 컨벀μ κ°μ λꡬ
-
- μ΄λμ λΌμ΄μ§ κ°μ μ© μ½λ© μ€νμΌ κ°μ λΌμ΄λΈλ¬λ¦¬
- νμ€μ 100μλ₯Ό λμ§ μλλ‘ ν¨.
- μλ 120μμλλ°.. μν λͺ¨λν°μμ μΌμͺ½ λΈλΌμ°μ λ₯Ό μ° μν©μμ 120μκΉμ§ νμλμ§ μμ.
- Xcodeμ Preferences -> Text Editing -> Displayμ Page guide at column μμ 100μΌλ‘ μ€μ .
- μ½λ‘ (
:)μ μΈ λμλ μ½λ‘ μ μ€λ₯Έμͺ½μ λ§ κ³΅λ°±μ λ . - λ¨, μΌν μ°μ°μμ κ²½μ° μλ€λ‘ λμ.
let name: String
let result = one > other ? true : false- μ°μ°μ μ€λ²λ‘λ© ν¨μ μ μμμλ μ°μ°μμ κ΄νΈ μ¬μ΄μ νμΉΈμ 곡백 νμ.
func ** (one: Int, other: Int)- ν¨μμ μ μκ° μ΅λ κΈΈμ΄λ₯Ό μ΄κ³Όνλ κ²½μ°, νλΌλ―Έν°λ₯Ό κΈ°μ€μΌλ‘ μ€ λ°κΏ.
func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
// doSomething()
}
func animationController(
forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
// doSomething()
}- ν¨μ νΈμΆ μ½λ λν μμ κ°μ.
let actionSheet = UIActionSheet(
title: "μ λ§ κ³μ μ μμ νμ€ κ±΄κ°μ?",
delegate: self,
cancelButtonTitle: "μ·¨μ",
destructiveButtonTitle: "μμ ν΄μ£ΌμΈμ"
)- νλΌλ―Έν°μ ν΄λ‘μ κ° 2κ° μ΄μ μ‘΄μ¬νλ κ²½μ°μλ 무쑰건 λ΄λ €μ°κΈ°.
UIView.animate(
withDuration: 0.25,
animations: {
// doSomething()
},
completion: { finished in
// doSomething()
}
)- λΉ μ€μλ κ³΅λ°±μ΄ ν¬ν¨λμ§ μλλ‘ ν¨.
- λͺ¨λ νμΌμ λΉ μ€λ‘ λλλλ‘ ν¨.
- MARK ꡬ문μ μμ μλμλ κ³΅λ°±μ΄ νμ.
// MARK: Layout
override func layoutSubviews() {
// doSomething()
}
// MARK: Actions
override func menuButtonDidTap() {
// doSomething()
}- μ½μ΄λ λ€μ΄λ°μ κ°μ₯ 첫λ²μ§Έλ₯Ό μ μΈνκ³ λ, λλ¬Έμλ‘ μ²λ¦¬
- λ³μλͺ
μ΄
idκ°μ κ²½μ°μλ§ μλ¬Έμλ‘ μ²λ¦¬
- λ³μλͺ
μ΄
- μ΄λ¦μλ UpperCamelCaseλ₯Ό μ¬μ©.
- μ½μ΄λ μμΈλ‘, λλ¬Έμ μ²λ¦¬
- ν΄λμ€ μ΄λ¦μ μ λμ¬λ₯Ό λΆμ΄μ§ μμ.
- νμνμ μμ리λ±μ μ λμ¬λ₯Ό λΆμ΄μ§ μμ.
/// μ’μ μ
class ViewController: ActionViewController { }
/// λμ μ
class AViewController: ActionViewController { }
/// μ’μ μ
let userID
let id
user.secondID = "test2"
/// λμ μ
let userId
let iD
user.secondId = "test2"
user.secondid = "test2"- ν¨μ μ΄λ¦μλ lowerCamelCaseλ₯Ό μ¬μ©.
- ν¨μμ΄λ¦μλ λλλ‘μ΄λ©΄ get, setκ°μ λͺ μΉμ μ°μ§ μμΌλ©°, ν΄λμ€ λ©μλμΈ κ²½μ° μ΄λ¦μ΄ μ€λ³΅λμ§ μλλ‘ μ²λ¦¬.
- λ§€κ°λ³μ λν λλλ‘ κ°λ¨ν μ λμ¬λ₯Ό μ΄μ©νμ¬ μ²λ¦¬.
/// μ’μ μ
func name(for user: User) -> String?
func grade(from: Subject) -> User?
/// λμ μ
func getName(user: User) -> String?
func getGradeFromSubject(subject: Subject) -> User?- enumμ κ° caseμλ lowerCamelCaseλ₯Ό μ¬μ©.
/// μ’μ μ
enum Result {
case success
case failure
}
/// λμ μ
enum Result {
case Success
case Failure
}- μ½μ΄μ κ²½μ°, μμμ λκ°μ΄ μλ¬Έμλ‘ νκΈ°νλ, μμμ΄ μλ κ²½μ° λ¬΄μ‘°κ±΄ λλ¬Έμλ‘ νκΈ°.
/// μ’μ μ
let userID: String
let html: String
let websiteURL: URL
let urlString: String
/// λμ μ
let userId: String
let HTML: String
let websideUrl: URL
let URLString: String- νλΌλ―Έν°μ 리ν΄νμ μ΄ μλ Closure μ μ μ, () -> Voidλ₯Ό μ΄μ©.
- Closure μ μ μ νλΌλ―Έν°μλ κ΄νΈλ₯Ό μ¬μ©νμ§ μμ.
- Closure μ μ μ κ°λ₯ν κ²½μ° νμ
μ μ μλ΅, μ¬μ©νμ§ μλ νλΌλ―Έν° λν
_λ‘ νκΈ° - νΈμΆ μ λ§μ§λ§ νλΌλ―Έν°κ° ClosureμΈ κ²½μ° νλΌλ―Έν° μ΄λ¦ μλ΅.
/// () -> Voidμ μ
/// μ’μ μ
let completion: (() -> Void)?
/// λμ μ
let completion: (() -> ())?
let completion: ((Void) -> (Void))?
/// κ΄νΈ κ΄λ ¨ μ
/// μ’μ μ
{ operation, response in
}
/// λμ μ
{ (operation, response) in
}
/// νμ
μ μ κ΄λ ¨ μ
/// μ’μ μ
{ _, result -> Void in
print(result)
}
/// λμ μ
{ (statusCode: Int, result: JSON) -> Void in
print(result)
}
/// λ§μ§λ§ νλΌλ―Έν°
/// μ’μ μ
UIView.animate(withDuration: 0.5) {
// doSomething
}
/// λμ μ
UIView.animate(withDuration: 0.5, animations: { () -> Void in
// doSomething
})- ν΄λμ€μ ꡬ쑰체 λ΄λΆμμλ selfλ₯Ό λͺ μμ μΌλ‘ μ¬μ©.
- ꡬ쑰체λ₯Ό μμ±ν λμλ Swift ꡬ쑰체 μμ±μλ₯Ό μ¬μ©.
- build, makeκ°μ λ©μλ μ¬μ© μ§μ
Array<T>,Dictionary<T: U>보λ€λ[T],[T: U]λ₯Ό μ¬μ©.
///λ₯Ό μ¬μ©νμ¬ λ¬Έμνμ μ¬μ©λλ μ£Όμ λ¨κΉ.// MARK:λ₯Ό μ¬μ©νμ¬ μ°κ΄ μ½λ ꡬλΆ.
- κ°λ₯νλ€λ©΄ λ³μλ₯Ό μ μν λ ν¨κ» μ΄κΈ°ν. μ΄λ Thenμ μ΄μ©.
/// μ’μ μ
let label = UILabel().then {
$0.textAlignment = .center
$0.textColor = .black
$0.text = "Hello, World"
}
/// λμ μ
let label = UILabel()
label.textAlignment = .center
label.textColor = .black
label.text = "Hell World"- extensionμ λλλ‘ λΆλ¦¬.
/// μ’μ μ
class ViewController: UIViewController {
// ...
}
// MARK: - ν
μ΄λΈλ·° Extension
extension ViewController: UITableViewDataSource {
// ...
}
extension ViewController: UITableViewDelegate {
// ...
}
// MARK: - μ½λ μ
λ·° Extension
extension ViewController: UICollectionViewDataSource {
// ...
}
extension ViewController: UICollectionViewDelegate {
// ...
}
/// λμ μ
class ViewController: UIViewController, UITableViewDataSource, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate {
// ...
}- SwiftLintλ₯Ό λ¨Όμ μ€μΉ
- xcode νλ‘μ νΈ μ€ν¬λ¦½νΈμμ, μλ μ€ν¬λ¦½νΈλ₯Ό μΆκ°.
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi- M1μ κ²½μ°
if test -d "/opt/homebrew/bin/"; then
PATH="/opt/homebrew/bin/:${PATH}"
fi
export PATH
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi- νλ‘μ νΈμ .swiftlint.yml νμΌμ λ§λ ν, μλ μ½λ μΆκ°
parent_config: https://raw.githubusercontent.com/HEROHJK/SwiftStyleGuide/master/lint.yml