-
Notifications
You must be signed in to change notification settings - Fork 3
Code Convention
StyleShare - swift style guideΒ μ κΈ°λ³ΈμΌλ‘ λ°λΌκ°λ€.
StyleShare μ Swift Style Guide λ₯Ό κΈ°λ³ΈμΌλ‘ ν΄μ μΆκ°νκ±°λ μμ νμμ΅λλ€.
SwiftLint λ₯Ό ν΅ν΄μ ν΅μΌμ±μλ ν΄λ¦°μ½λλ₯Ό μΆκ΅¬ν©λλ€.
- UpperCamelCase μ¬μ©
- lowerCamelCase μ¬μ©νκ³ λμ¬λ‘ μμ
- λμ¬μν + λͺ©μ μ΄ ex) touchBackButton
- pop, push, present, dismiss
- λμ¬ + To + λͺ©μ μ§ λ·° (λ€μμ λ³΄μΌ λ·°)
- dismissλ dismiss + νμ¬ λ·°
- register + λͺ©μ μ΄ ex) registerXib
- μλΉμ€ν¨μλͺ
+ WithAPI ex) getListWithAPI
- λμ¬μν + λͺ©μ μ΄ + WithAnimation ex)showButtonsWithAnimation
delegate λ©μλλ νλ‘ν μ½λͺ μΌλ‘ λ€μμ€νμ΄μ€λ₯Ό ꡬλΆ
μ’μ μ:
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)
}
- lowerCamelCase μ¬μ©
- κ° case μλ lowerCamelCase μ¬μ©
μ½μ΄λ‘ μμνλ κ²½μ° μλ¬Έμλ‘ νκΈ°, κ·Έ μΈμλ νμ λλ¬Έμ
// μ’μ μ:
let userID: Int?
let html: String?
let websiteURL: URL?
let urlString: String?
// λμ μ:
let userId: Int?
let HTML: String?
let websiteUrl: NSURL?
let URLString: String?
- μ€μ λ©μλλ
set~
μΌλ‘ μμ. ex) setUI, setData, setList
https://github.com/SOPT-29th-Joint-Seminar-10/Client-iOS/wiki/_new
- ViewController β
VC
- TableViewCell β
TVC
- CollectionViewCell β
CVC
-
λ€μ¬μ°κΈ°μλ ν(tab) λμ 2κ°μ spaceλ₯Ό μ¬μ©ν©λλ€.
-
μ½λ‘ (
:
)μ μΈ λμλ μ½λ‘ μ μ€λ₯Έμͺ½μλ§ κ³΅λ°±μ λ‘λλ€.let names: [String: String]?
-
μ°μ°μ μ€λ²λ‘λ© ν¨μ μ μμμλ μ°μ°μμ κ΄νΈ μ¬μ΄μ ν μΉΈ λμ΄μλλ€.
func ** (lhs: Int, rhs: 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() } )
-
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() }
λͺ¨λ μν¬νΈλ μνλ²³ μμΌλ‘ μ λ ¬ν©λλ€. λ΄μ₯ νλ μμν¬λ₯Ό λ¨Όμ μν¬νΈνκ³ , λΉ μ€λ‘ ꡬλΆνμ¬ μλνν° νλ μμν¬λ₯Ό μν¬νΈν©λλ€.
import UIKit
import SwiftyColor
import SwiftyImage
import Then
import URLNavigator
-
νλΌλ―Έν°μ λ¦¬ν΄ νμ μ΄ μλ Closure μ μμμλΒ
() -> Void
λ₯Ό μ¬μ©ν©λλ€.μ’μ μ:
let completionBlock: (() -> Void)?
λμ μ:
let completionBlock: (() -> ())? let completionBlock: ((Void) -> (Void))?
-
Closure μ μμ νλΌλ―Έν°μλ κ΄νΈλ₯Ό μ¬μ©νμ§ μμ΅λλ€.
μ’μ μ:
{ 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() })
μ½λλ κ°λ₯νλ©΄ μ체μ μΌλ‘ λ¬Έμκ° λμ΄μΌ νλ―λ‘, μ½λμ ν¨κ» μλ μΈλΌμΈ(inline) μ£Όμμ νΌνλ€.
// MARK: - Properties
// MARK: - @IBOutlet Properties
// MARK: - @IBAction Properties
// MARK: - View Life Cycle
// MARK: - Extensions
> setUI, registerXib, assignDelegate λ±
// MARK: - Custom Method
// MARK: - @objc Function
// MARK: - Network
> λ€νΈμν¬ λͺ©μ μ κ°μ§ ν¨μλ€
// TODO: -
> ν΄μΌνλ κ²
// FIXME: -
> κ³ μΉ μμ
컀μ€ν λ² μλ, νλ‘ν μ½, ν΄λμ€μ κ²½μ°μ ν΅ν¬ν μ£Όμ λ¬κΈ°
/// (μ머리 λΆλΆ)
/// (λμ€ν¬λ¦½μ
λΆλΆ)
class MyClass {
let myProperty: Int
init(myProperty: Int) {
self.myProperty = myProperty
}
}
/**
summary
- note: μ€λͺ
- parameters:
- property: νλ‘νΌν°
- throws: μ€λ₯κ° λ°μνλ©΄ customErrorμ ν μΌμ΄μ€λ₯Ό throw
- returns: "\\(name)λ ~" String
*/
func printProperty(property: Int) {
print(property)
}
- μ°Έκ³ :
[iOS - swift] μ£Όμ μ²λ¦¬, ν΅ ν¬ν μ£Όμ, //, ///
[Swift] Xcode ν΅ν¬ν λ° λ§ν¬μ λ¬Έλ²
μ’μ μ:
let name: String = "μ² μ"
let height: Float = "10.0"
λμ μ:
let name = "μ² μ"
let height = "10.0"
νλ‘ν μ½μ μ μ©ν λμλ extensionμ λ§λ€μ΄μ κ΄λ ¨λ λ©μλλ₯Ό λͺ¨μλ‘λλ€.
μ’μ μ:
final class MyViewController: UIViewController {
// ...
}
// MARK: - UITableViewDataSource
extension MyViewController: UITableViewDataSource {
// ...
}
// MARK: - UITableViewDelegate
extension MyViewController: UITableViewDelegate {
// ...
}
λμ μ:
final class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// ...
}****
-
self
λ μ΅λν μ¬μ©μΒ μ§μ -
viewDidLoad()
μμλΒ ν¨μνΈμΆλ§ - delegate μ§μ , UIκ΄λ ¨ μ€μ λ±λ± λͺ¨λ ν¨μμ μν μ λ°λΌμ extension μΌλ‘ λΉΌκΈ°
- νμμλ μ£Όμλ€ μ κ±°
identifier λ μ»¬λ¬ ν°νΈ