https://github.com/StyleShare/swift-style-guide
주석은 없습니다 → 클린코드 지향
줄내림 control + m
private let developerNameLabel: UILabel = {
let label = UILabel()
label.text = "Developer"
label.font = .systemFont(ofSize: 14)
label.textColor = .tintColor
return label
}()
들여쓰기 및 띄어쓰기
들여쓰기에는 탭(tab) 대신 2개의 space를 사용합니다.
콜론(:
)을 쓸 때에는 콜론의 오른쪽에만 공백을 둡니다.
let names: [String: String]?
연산자 오버로딩 함수 정의에서는 연산자와 괄호 사이에 한 칸 띄어씁니다.
func ** (lhs: Int, rhs: Int)
줄바꿈
함수의 매개변수가 2개 이상인 경우에는 아래와 같이 줄바꿈합니다.
func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
// doSomething()
}
func animationController(
forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
// doSomething()
}
호출하는 함수의 매개변수의 수가 2개 이상인 경우에는 파라미터 이름을 기준으로 줄바꿈합니다.
let actionSheet = UIActionSheet(
title: "정말 계정을 삭제하실 건가요?",
delegate: self,
cancelButtonTitle: "취소",
destructiveButtonTitle: "삭제해주세요"
)
단, 파라미터에 클로저가 2개 이상 존재하는 경우에는 무조건 내려쓰기합니다.
UIView.animate(
withDuration: 0.25,
animations: {
// doSomething()
},
completion: { finished in
// doSomething()
}
)
if let
구문이 길 경우에는 줄바꿈하고 한 칸 들여씁니다.
if let user = self.veryLongFunctionNameWhichReturnsOptionalUser(),
let name = user.veryLongFunctionNameWhichReturnsOptionalName(),
user.gender == .female {
// ...
}
guard let
구문이 길 경우에는 줄바꿈하고 한 칸 들여씁니다. else
는 guard
와 같은 들여쓰기를 적용합니다.
guard let user = self.veryLongFunctionNameWhichReturnsOptionalUser(),
let name = user.veryLongFunctionNameWhichReturnsOptionalName(),
user.gender == .female
else {
return
}
빈 줄
빈 줄에는 공백이 포함되지 않도록 합니다.
모든 파일은 빈 줄로 끝나도록 합니다.
MARK 구문 위와 아래에는 공백이 필요합니다.
// MARK: Layout
override func layoutSubviews() {
// doSomething()
}
// MARK: Actions
override func menuButtonDidTap() {
// doSomething()
}