-
Notifications
You must be signed in to change notification settings - Fork 0
πΆ WALWAL Code Convention
- λ€μ¬μ°κΈ°μλ ν(tab) λμ 2κ°μ spaceλ₯Ό μ¬μ©ν©λλ€.

-
μ½λ‘ (
:
)μ μΈ λμλ μ½λ‘ μ μ€λ₯Έμͺ½μλ§ κ³΅λ°±μ λ‘λλ€.let names: [String: String]?
-
μ°μ°μ μ€λ²λ‘λ© ν¨μ μ μμμλ μ°μ°μμ κ΄νΈ μ¬μ΄μ ν μΉΈ λμ΄μλλ€.
func ** (lhs: Int, rhs: Int)
-
νλΌλ―Έν° 3κ° μ΄μμΌ λ, νλΌλ―Έν° κΈ°μ€μΌλ‘ μ€λ°κΏν©λλ€.
let actionSheet = UIActionSheet( title: "μ λ§ κ³μ μ μμ νμ€ κ±΄κ°μ?", delegate: self, cancelButtonTitle: "μ·¨μ", destructiveButtonTitle: "μμ ν΄μ£ΌμΈμ" )
-
νλΌλ―Έν°μ ν΄λ‘μ κ° 2κ° μ΄μ μ‘΄μ¬νλ κ²½μ°μλ 무쑰건 λ΄λ €μ°κΈ°ν©λλ€.
UIView.animate( withDuration: 0.25, animations: { // doSomething() }, completion: { finished in // doSomething() } )
-
ν¨μλ₯Ό μμ±ν λ, 첫 λ²μ§Έ νλΌλ―Έν°κ° μ€λ°κΏ μ΄νμ μμΉνλλ‘ ν©λλ€.
func animationController( forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController ) -> UIViewControllerAnimatedTransitioning? { // doSomething() }
-
if let
Β κ΅¬λ¬Έμ΄ κΈΈ κ²½μ°μλ μ€λ°κΏνκ³ ν μΉΈ λ€μ¬μλλ€.if let user = self.veryLongFunctionNameWhichReturnsOptionalUser(), let name = user.veryLongFunctionNameWhichReturnsOptionalName(), user.gender == .female { // ... }
-
guard let
Β κ΅¬λ¬Έμ΄ κΈΈ κ²½μ°μλ μ€λ°κΏνκ³ ν μΉΈ λ€μ¬μλλ€.Βelse
λΒ λ¨μreturn
μΈ κ²½μ°μλ ν μ€λ‘ μ°κ³else
ꡬ문 μμ λμμ΄ μλ κ²½μ° μ€λ°κΏν©λλ€. (μμ μ°Έκ³ )guard let user = self.veryLongFunctionNameWhichReturnsOptionalUser(), let name = user.veryLongFunctionNameWhichReturnsOptionalName(), user.gender == .female else { return } guard let user = self.veryLongFunctionNameWhichReturnsOptionalUser(), let name = user.veryLongFunctionNameWhichReturnsOptionalName(), user.gender == .female else { // ... return }
-
return
μ μ μ€λ°κΏνμ§ μμ΅λλ€.
-
λΉ μ€μλ κ³΅λ°±μ΄ ν¬ν¨λμ§ μλλ‘ ν©λλ€.
-
λͺ¨λ νμΌμ λΉ μ€λ‘ λλλλ‘ ν©λλ€.
-
MARK ꡬ문 μμ μλμλ κ³΅λ°±μ΄ νμν©λλ€.
// MARK: - Layout override func layoutSubviews() { // doSomething() } // MARK: - Actions override func menuButtonDidTap() { // doSomething() }
-
λ΄μ₯ νλ μμν¬λ₯Ό λ¨Όμ μν¬νΈνκ³ , λΉ μ€λ‘ ꡬλΆνμ¬ μλνν° νλ μμν¬λ₯Ό μν¬νΈν©λλ€.
import UIKit import SwiftyColor import SwiftyImage import Then import URLNavigator
-
ν΄λμ€μ ꡬ쑰체μ μ΄λ¦μλ UpperCamelCaseλ₯Ό μ¬μ©ν©λλ€.
β μ’μ μ:
class SomeClass { // class definition goes here } struct SomeStructure { // structure definition goes here }
β λμ μ:
class someClass { // class definition goes here } struct someStructure { // structure definition goes here }
-
ν¨μ μ΄λ¦μλ lowerCamelCaseλ₯Ό μ¬μ©ν©λλ€.
-
ν¨μ μ΄λ¦ μμλ λλλ‘μ΄λ©΄Β
get
,make
,set
μ λΆμ΄μ§ μμ΅λλ€. -
ν¨μ μ΄λ¦μ
return
κ°μ λ³μμ²λΌ μ¬μ©νλ€λ©΄, λͺ μ¬λ‘ μ¬μ©ν΄λ 무κ΄β μ’μ μ:
func name(for user: User) -> String?
β λμ μ:
func getName(for user: User) -> String?
-
Action ν¨μμ λ€μ΄λ°μ 'μ£Όμ΄ + λμ¬ + λͺ©μ μ΄' ννλ₯Ό μ¬μ©ν©λλ€.
- Tap(λλ λ€ λ)*μΒ
UIControlEvents
μΒ.touchUpInside
μ λμνκ³ , *Press(λλ¦)*λΒ.touchDown
μ λμν©λλ€.- λμ¬ κ³Όκ±°νμΌλ‘ (ex.
*confirmButtonTapped*
) -
*will~*
μ νΉμ νμκ° μΌμ΄λκΈ° μ§μ - boolνμ λ°ννλ ν¨μλ
*is~*
- λμ¬ κ³Όκ±°νμΌλ‘ (ex.
β μ’μ μ:
func backButtonDidTap() { // ... }
β λμ μ:
func back() { // ... } func pressBack() { // ... }
- Tap(λλ λ€ λ)*μΒ
- λ³μ μ΄λ¦μλ lowerCamelCaseλ₯Ό μ¬μ©ν©λλ€.
-
μμ μ΄λ¦μλ lowerCamelCaseλ₯Ό μ¬μ©ν©λλ€.
β μ’μ μ:
let maximumNumberOfLines = 3
β λμ μ:
let MaximumNumberOfLines = 3 let MAX_LINES = 3
-
enumμ μ΄λ¦μλ UpperCamelCaseλ₯Ό μ¬μ©ν©λλ€.
-
enumμ κ° caseμλ lowerCamelCaseλ₯Ό μ¬μ©ν©λλ€.
-
caseμ μ μΈλΆλ λΌμΈλ³ 1κ°μ caseλ₯Ό μμ νκ³ , caseμ λν μ²λ¦¬κ° λμΌν κ²½μ°μλ ν μ€μ μ¬λ¬ caseμ λν κ²½μ°λ₯Ό μ²λ¦¬
β μ’μ μ:
enum Result { case signUp(body: String) case signIn var httpMethod: { switch self { case .signUp, .signIn: return .POST } } }
β λμ μ:
enum Result { case .Success case .Failure } enum result { case .Success case .Failure }
-
νλ‘ν μ½μ μ΄λ¦μλ UpperCamelCaseλ₯Ό μ¬μ©ν©λλ€.
-
ꡬ쑰체λ ν΄λμ€μμ νλ‘ν μ½μ μ±νν λλ μ½λ‘ κ³Ό λΉμΉΈμ λ£μ΄ ꡬλΆνμ¬ λͺ μν©λλ€.
-
extensionμ ν΅ν΄ μ±νν λλ λμΌνκ² μ μ©λ©λλ€.
-
protocolμ suffixλ
~Protocol
λ‘ ν΅μΌν©λλ€.β μ’μ μ:
protocol SomeProtocol { // protocol definition goes here } struct SomeStructure: SomeProtocol, AnotherProtocol { // structure definition goes here } class SomeClass: SomeSuperclass, SomeProtocol, AnotherProtocol { // class definition goes here } extension UIViewController: SomeProtocol, AnotherProtocol { // doSomething() }
-
μ½μ΄λ‘ μμνλ κ²½μ°(prefixμ κ²½μ°) μλ¬Έμλ‘ νκΈ°νκ³ , κ·Έ μΈμ κ²½μ°μλ νμ λλ¬Έμλ‘ νκΈ°ν©λλ€.
β μ’μ μ:
let userID: Int? let html: String? let websiteURL: URL? let urlString: String?
β λμ μ:
let userId: Int? let HTML: String? let websiteUrl: NSURL? let URLString: String?
-
Delegate λ©μλλ νλ‘ν μ½λͺ μΌλ‘ λ€μμ€νμ΄μ€λ₯Ό ꡬλΆν©λλ€.
-
ex)
UserCellDelegate
β λ΄λΆ ν¨μμ prefixλUserCell
β μ’μ μ:
protocol UserCellDelegate { func userCellDidSetProfileImage(_ cell: UserCell) func userCell(_ cell: UserCell, didTapFollowButtonWith user: User) }
β λμ μ:
protocol UserCellDelegate { func didSetProfileImage() func followPressed(user: User) // `UserCell`μ΄λΌλ ν΄λμ€κ° μ‘΄μ¬ν κ²½μ° μ»΄νμΌ μλ¬ λ°μ func UserCell(_ cell: UserCell, didTapFollowButtonWith user: User) }
-
νλΌλ―Έν°μ λ¦¬ν΄ νμ μ΄ μλ Closure μ μμμλΒ
() -> Void
λ₯Ό μ¬μ©ν©λλ€.β μ’μ μ:
let completionBlock: (() -> Void)?
β λμ μ:
let completionBlock: (() -> ())?let completionBlock: ((Void) -> (Void))?
-
Closure μ μμ νλΌλ―Έν°μλ κ΄νΈλ₯Ό μ¬μ©νμ§ μμ΅λλ€. Rx λ΄λΆ ν΄λ‘μ μμλ μ μ©λ©λλ€.
β μ’μ μ:
{ operation, responseObject in // doSomething() }
β λμ μ:
{ (operation, responseObject) in // doSomething() }
-
Closure μ μμ κ°λ₯ν κ²½μ° νμ μ μλ₯Ό μλ΅ν©λλ€.
β μ’μ μ:
..., completion: { finished in // doSomething() }
β λμ μ:
..., completion: { (finished: Bool) -> Void in // doSomething() }
-
Closure νΈμΆμ λλ€λ₯Έ μ μΌν Closureλ₯Ό λ§μ§λ§ νλΌλ―Έν°λ‘ λ°λ κ²½μ°, νλΌλ―Έν° μ΄λ¦μ μλ΅ν©λλ€.
β μ’μ μ:
UIView.animate(withDuration: 0.5) { // doSomething() }
β λμ μ:
UIView.animate(withDuration: 0.5, animations: { () -> Void in // doSomething() })
-
ꡬ쑰체λ₯Ό μμ±ν λμλ Swift ꡬ쑰체 μμ±μλ₯Ό μ¬μ©ν©λλ€.
β μ’μ μ:
let frame = CGRect(x: 0, y: 0, width: 100, height: 100)
β λμ μ:
let frame = CGRectMake(0, 0, 100, 100)
-
Array<T>
μΒDictionary<T: U>
 보λ€λΒ[T]
,Β[T: U]
λ₯Ό μ¬μ©ν©λλ€.β μ’μ μ:
var messages: [String]? var names: [Int: String]?
β λμ μ:
var messages: Array<String>? var names: Dictionary<Int, String>?
-
///
λ₯Ό μ¬μ©ν΄μ λ¬Έμνμ μ¬μ©λλ μ£Όμμ λ¨κΉλλ€./// μ¬μ©μ νλ‘νμ κ·Έλ €μ£Όλ λ·° class ProfileView: UIView { /// μ¬μ©μ λλ€μμ κ·Έλ €μ£Όλ λΌλ²¨ var nameLabel: UILabel! }
-
// MARK:
λ₯Ό μ¬μ©ν΄μ μ°κ΄λ μ½λλ₯Ό ꡬλΆμ§μ΅λλ€.
Objective-Cμμ μ 곡νλΒ#pragma mark
μ κ°μ κΈ°λ₯μΌλ‘, μ°κ΄λ μ½λμ κ·Έλ μ§ μμ μ½λλ₯Ό ꡬλΆν λ μ¬μ©ν©λλ€.// MARK: - Init override init(frame: CGRect) { // doSomething() } deinit { // doSomething() } // MARK: - Layout override func layoutSubviews() { // doSomething() } // MARK: - Actions override func menuButtonDidTap() { // doSomething() }
-
κ°λ₯νλ€λ©΄ λ³μλ₯Ό μ μν λ ν¨κ» μ΄κΈ°ννλλ‘ ν©λλ€.Β Thenμ μ¬μ©νλ©΄ μ΄κΈ°νμ ν¨κ» μμ±μ μ§μ ν μ μμ΅λλ€.
let label = UILabel().then { $0.textAlignment = .center $0.textColor = .black $0.text = "Hello, World!" }
-
μμλ₯Ό μ μν λμλΒ
enum
λ₯Ό λ§λ€μ΄ λΉμ·ν μμλΌλ¦¬ λͺ¨μλ‘λλ€. μ¬μ¬μ©μ±κ³Ό μ μ§λ³΄μ μΈ‘λ©΄μμ ν° ν₯μμ κ°μ Έμ΅λλ€.Βstruct
Β λμ Βenum
μ μ¬μ©νλ μ΄μ λ, μμ±μκ° μ 곡λμ§ μλ μλ£νμ μ¬μ©νκΈ° μν΄μμ λλ€.Β CGFloatLiteralκ³ΌΒ SwiftyColorλ₯Ό μ¬μ©ν΄μ μ½λλ₯Ό λ¨μνμν΅λλ€.final class ProfileViewController: UIViewController { private enum Metric { static let profileImageViewLeft = 10.f static let profileImageViewRight = 10.f static let nameLabelTopBottom = 8.f static let bioLabelTop = 6.f } private enum Font { static let nameLabel = UIFont.boldSystemFont(ofSize: 14) static let bioLabel = UIFont.boldSystemFont(ofSize: 12) } private enum Color { static let nameLabelText = 0x000000.color static let bioLabelText = 0x333333.color ~ 70% } }
μ΄λ κ² μ μΈλ μμλ€μ λ€μκ³Ό κ°μ΄ μ¬μ©λ μ μμ΅λλ€.
self.profileImageView.frame.origin.x = Metric.profileImageViewLeft self.nameLabel.font = Font.nameLabel self.nameLabel.textColor = Color.nameLabelText
-
λμ΄μ μμμ΄ λ°μνμ§ μλ ν΄λμ€λ νμΒ
final
Β ν€μλλ‘ μ μΈν©λλ€. -
νλ‘ν μ½μ μ μ©ν λμλ extensionμ λ§λ€μ΄μ κ΄λ ¨λ λ©μλλ₯Ό λͺ¨μλ‘λλ€.
-
private method
λextension
μΌλ‘ λΆλ¦¬ν©λλ€.β μ’μ μ:
final class MyViewController: UIViewController { // ... } // MARK: - UITableViewDataSource extension MyViewController: UITableViewDataSource { // ... } // MARK: - UITableViewDelegate extension MyViewController: UITableViewDelegate { // ... } // MARK: - Private Method extension MyViewController { private func goHome() { /// do something } }
β λμ μ:
final class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { // ... }
-
ꡬ문μ 첫λ²μ§Έ
Operator
κΈ°μ€μΌλ‘ μ€μ λ°κΏλλ€.comfirmButton.rx.tap .map{ } .bind() .disposed(by: disposeBag) buttonTapped .map { Reactor.Action.tapped } .bind(to: reactor.action) .disposed(by: disposeBag) reactor.state .map { $0.id } .disposed(by: disposeBag) input.settingButtonTapped .withUnretained(self) .subscribe(onNext: { owner, _ in owner.coordinator?.destination.accept(.myPage) }) .disposed(by: disposeBag)
-
.withUnretained(self)
λ driveμ μ¬μ©ν μ μμ΅λλ€. -
λ°λΌμ
.drive(with: self)
,.subscribe(with: self)
μ μ¬μ©νλλ‘ ν©λλ€. -
self
λ₯Ό μ§μΉνλ λ³μλowner
λ‘ μμ±ν©λλ€.β μ’μ μ:
reactor.state .map { $0.logoutSuccess } .filter { $0 } .asDriver(onErrorJustReturn: false) .drive(with: self) { owner, _ in } .disposed(by: disposeBag)
// Case 1
extension Reactive where Base: CustomButton {
public var tap: ControlEvent<Void> {
let source: Observable<Void> = self.base.rx.tapGesture()
.when(.recognized)
.map { _ in () }
return ControlEvent(events: source)
}
}
// Case 2
extension Reactive where Base: UIView {
public var tapped() -> ControlEvent<Void> {
let source = self.base.rx.tapGesture().when(.recognized).map { _ in }
return ControlEvent(events: source)
}
}
StyleShare κ°μ΄λλ₯Ό μ°Έκ³ νμ¬ μμ±νμμ΅λλ€. πStyleShare Guide