-
Notifications
You must be signed in to change notification settings - Fork 0
InventoryBox_Coding_Convention
StyleShare ๊ตฌ์ฑ์๋ค์ด Swift ์ฝ๋๋ฅผ ์ดํดํ๊ธฐ ์ฝ๊ณ ๋ช ํํ๊ฒ ์์ฑํ๊ธฐ ์ํ ์คํ์ผ ๊ฐ์ด๋์ ๋๋ค. ๊ตฌ์ฑ์๋ค์ ์์ฌ๊ฒฐ์ ์ ๋ฐ๋ผ ์์๋ก ๋ณ๊ฒฝ๋ ์ ์์ต๋๋ค.
๋ณธ ๋ฌธ์์ ๋์์์ง ์์ ๊ท์น์ ์๋ ๋ฌธ์๋ฅผ ๋ฐ๋ฆ ๋๋ค.
- ์ฝ๋ ๋ ์ด์์
- ๋ค์ด๋ฐ
- ํด๋ก์
- ํด๋์ค์ ๊ตฌ์กฐ์ฒด
- ํ์
- ์ฃผ์
- ํ๋ก๊ทธ๋๋ฐ ๊ถ์ฅ์ฌํญ
-
๋ค์ฌ์ฐ๊ธฐ์๋ ํญ(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 }
-
ํ ์ค์ ์ต๋ 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- ํด๋์ค ์ด๋ฆ์๋ UpperCamelCase๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- ํด๋์ค ์ด๋ฆ์๋ ์ ๋์ฌPrefix๋ฅผ ๋ถ์ด์ง ์์ต๋๋ค.
-
ํจ์ ์ด๋ฆ์๋ lowerCamelCase๋ฅผ ์ฌ์ฉํฉ๋๋ค.
-
ํจ์ ์ด๋ฆ ์์๋ ๋๋๋ก์ด๋ฉด
get์ ๋ถ์ด์ง ์์ต๋๋ค.์ข์ ์:
func name(for user: User) -> String?
๋์ ์:
func getName(for user: User) -> String?
-
Action ํจ์์ ๋ค์ด๋ฐ์ '์ฃผ์ด + ๋์ฌ + ๋ชฉ์ ์ด' ํํ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- *Tap(๋๋ ๋ค ๋)*์
UIControlEvents์.touchUpInside์ ๋์ํ๊ณ , *Press(๋๋ฆ)*๋.touchDown์ ๋์ํฉ๋๋ค. - will~์ ํน์ ํ์๊ฐ ์ผ์ด๋๊ธฐ ์ง์ ์ด๊ณ , did~๋ ํน์ ํ์๊ฐ ์ผ์ด๋ ์งํ์ ๋๋ค.
-
should~๋ ์ผ๋ฐ์ ์ผ๋ก
Bool์ ๋ฐํํ๋ ํจ์์ ์ฌ์ฉ๋ฉ๋๋ค.
์ข์ ์:
func backButtonDidTap() { // ... }
๋์ ์:
func back() { // ... } func pressBack() { // ... }
- *Tap(๋๋ ๋ค ๋)*์
- ๋ณ์ ์ด๋ฆ์๋ lowerCamelCase๋ฅผ ์ฌ์ฉํฉ๋๋ค.
-
์์ ์ด๋ฆ์๋ lowerCamelCase๋ฅผ ์ฌ์ฉํฉ๋๋ค.
์ข์ ์:
let maximumNumberOfLines = 3
๋์ ์:
let kMaximumNumberOfLines = 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?
-
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) }
-
ํ๋ผ๋ฏธํฐ์ ๋ฆฌํด ํ์ ์ด ์๋ 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() })
-
ํด๋์ค์ ๊ตฌ์กฐ์ฒด ๋ด๋ถ์์๋
self๋ฅผ ๋ช ์์ ์ผ๋ก ์ฌ์ฉํฉ๋๋ค. -
๊ตฌ์กฐ์ฒด๋ฅผ ์์ฑํ ๋์๋ 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์ ๋ง๋ค์ด์ ๊ด๋ จ๋ ๋ฉ์๋๋ฅผ ๋ชจ์๋ก๋๋ค.
์ข์ ์:
final class MyViewController: UIViewController { // ... } // MARK: - UITableViewDataSource extension MyViewController: UITableViewDataSource { // ... } // MARK: - UITableViewDelegate extension MyViewController: UITableViewDelegate { // ... }
๋์ ์:
final class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { // ... }
๋ณธ ๋ฌธ์๋ ํฌ๋ฆฌ์์ดํฐ๋ธ ์ปค๋จผ์ฆ ์ ์์ํ์ 4.0 ๊ตญ์ ๋ผ์ด์ผ์ค์ ๋ฐ๋ผ ์ด์ฉํ ์ ์์ผ๋ฉฐ, ์ ์๊ถ์ ์ ์์ด๊ณผ StyleShare์๊ฒ ์์ต๋๋ค.