Skip to content

Code Convention

HEE / ์ •์€ํฌ edited this page May 17, 2022 · 7 revisions

๐Ÿ‘ป StyleShare - swift style guideย ์„ ์ฐธ๊ณ ํ•˜์—ฌ ํ•จ๊ป˜ ํ† ์˜ํ–ˆ์œผ๋ฉฐ, ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์ฑ„ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.


โœจ ์ฝ”๋“œ ๋ ˆ์ด์•„์›ƒ

1. ๋“ค์—ฌ์“ฐ๊ธฐ ๋ฐ ๋„์›Œ์“ฐ๊ธฐ

  • ์ฝœ๋ก (:)์„ ์“ธ ๋•Œ์—๋Š” ์ฝœ๋ก ์˜ ์˜ค๋ฅธ์ชฝ์—๋งŒ ๊ณต๋ฐฑ์„ ๋‘ก๋‹ˆ๋‹ค.
let names: [String: String]?

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
}

ใ„ฑ. ์ตœ๋Œ€ ์ค„ ๊ธธ์ด

  • ํ•œ ์ค„์€ ์ตœ๋Œ€ 99์ž๋ฅผ ๋„˜์ง€ ์•Š์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    • Xcode์˜ Preferences โ†’ Text Editing โ†’ Display์˜ 'Page guide at column' ์˜ต์…˜์„ ํ™œ์„ฑํ™”ํ•˜๊ณ  99์ž๋กœ ์„ค์ •ํ•˜๋ฉด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

ใ„ด. ๋นˆ ์ค„

  • MARK ๊ตฌ๋ฌธ ์œ„์—๋Š” ๊ณต๋ฐฑ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
// MARK: Layout
override func layoutSubviews() {
  // doSomething()
}

// MARK: Actions
override func menuButtonDidTap() {
  // doSomething()
}

ใ„ท. ์ž„ํฌํŠธ

  • ๋ชจ๋“ˆ ์ž„ํฌํŠธ๋Š” ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ๋‚ด์žฅ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋จผ์ € ์ž„ํฌํŠธํ•˜๊ณ , ๋นˆ ์ค„๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์„œ๋“œํŒŒํ‹ฐ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ž„ํฌํŠธํ•ฉ๋‹ˆ๋‹ค.
import UIKit

import SwiftyColor
import SwiftyImage
import Then
import URLNavigator

โœจ ๋„ค์ด๋ฐ

1. ํด๋ž˜์Šค

  • ํด๋ž˜์Šค ์ด๋ฆ„์—๋Š” UpperCamelCase๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ํด๋ž˜์Šค ์ด๋ฆ„์—๋Š” ์ ‘๋‘์‚ฌPrefix๋ฅผ ๋ถ™์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

2. ํ•จ์ˆ˜

  • ํ•จ์ˆ˜ ์ด๋ฆ„์—๋Š” lowerCamelCase๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ํ•จ์ˆ˜ ์ด๋ฆ„ ์•ž์—๋Š” ๋˜๋„๋ก์ด๋ฉด get์„ ๋ถ™์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ข‹์€ ์˜ˆ:

func name(for user: User) -> String?

๋‚˜์œ ์˜ˆ:

func getName(for user: User) -> String?

3. ๋ณ€์ˆ˜

  • ๋ณ€์ˆ˜ ์ด๋ฆ„์—๋Š” lowerCamelCase๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

4. ์ƒ์ˆ˜

  • ์ƒ์ˆ˜ ์ด๋ฆ„์—๋Š” lowerCamelCase๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    • ์ข‹์€ ์˜ˆ
    let maximumNumberOfLines = 3
    • ๋‚˜์œ ์˜ˆ
    let MaximumNumberOfLines = 3
    let MAX_LINES = 3

ใ„ฑ. ์—ด๊ฑฐํ˜•

  • enum์˜ ๊ฐ case์—๋Š” lowerCamelCase๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    • ์ข‹์€ ์˜ˆ
    enum Result {
      case .success
      case .failure
    }
    • ๋‚˜์œ ์˜ˆ
    enum Result {
      case .Success
      case .Failure
    }

ใ„ด. ์•ฝ์–ด

  • ์•ฝ์–ด๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ ์†Œ๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•˜๊ณ , ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค.

    • ์ข‹์€ ์˜ˆ
    let userID: Int?
    let html: String?
    let websiteURL: URL?
    let urlString: String?
    • ๋‚˜์œ ์˜ˆ
    let userId: Int?
    let HTML: String?
    let websiteUrl: NSURL?
    let URLString: String?

5. Delegate

  • 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)
    }

6. ํด๋กœ์ €, ํด๋ž˜์Šค์™€ ๊ตฌ์กฐ์ฒด, ํƒ€์ž…, ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ถŒ์žฅ ์‚ฌํ•ญ

  • ๊ทธ๋Œ€๋กœ ์ฑ„ํƒ

โœจ ์ฃผ์„

  • //๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹ค๋ฅธ ํŒ€์›์˜ ์ฐธ๊ณ ์šฉ์œผ๋กœ ํ‘œ์‹œํ•  ์ฃผ์„์„ ๋‚จ๊น๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์–ด๋ ต๋‹ค๊ณ  ์˜ˆ์ธก๋˜๋Š” ๊ณณ์—๋งŒ ์ฃผ์„์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

    • ์˜ˆ์‹œ
    // ์‚ฌ์šฉ์ž ํ”„๋กœํ•„์„ ๊ทธ๋ ค์ฃผ๋Š” ๋ทฐ
    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()
    }