본문 바로가기
YAGOM CAREER STARTER

[TIL] 20230302: 함수배치순서, format specifier

by Rhode 2023. 3. 3.

본문은 야곰 아카데미 커리어 스타터 캠프를 통해 학습한 내용을 회고한 글입니다.


함수 배치 순서

  • 프로퍼티 -> init -> override func -> custom func 및 IBAction 의 순서로 배치한다.
  • 각각의 method들 중에서는 먼저 호출되는 method의 순서로 배치한다.

 

format specifier

String을 별도로 관리해주기 위하여 enum으로 NameSpace를 구현해주었습니다.

struct와 enum 모두 NameSpace를 만들 수 있습니다. 그렇지만, struct로 만들 경우 인스턴스 생성이 가능해집니다. 물론, private init() { }를 사용하면, 그것을 막을 수 있습니다. 그렇지만, enum을 사용하게되면 그러한 작업 없이도 불필요한 인스턴스 생성을 막을 수 있기 때문에 enum으로 만들었습니다.

문자열 중간에 변수가 들어가는 것을 처리하기 위하여 format specifier의 개념을 사용해주었습니다. String은 %@로 Int는 %d로 대체하여 문자열을 구성할 수 있습니다. 그 결과 다음과 같은 NameSpace를 만들 수 있었습니다:

enum NameSpace {
    static let emptyString = ""
    static let homeViewTitle = "메인"
    static let itemViewTitle = "한국의 출품작"
    static let visitorCountText = "방문객 : %@ 명"
    static let locationText = "개최지 : %@"
    static let durationText = "개최 기간 : %@"
}

%@에 해당하는 변수는 다음과 같이, 이 NameSpace를 사용할 때 지정해줄 수 있습니다:

titleLabel.text = expositionUniverselle.title
visitorCountLabel.text = String(format: NameSpace.visitorCountText, expositionUniverselle.visitorCount.insertComma())
locationLabel.text = String(format: NameSpace.locationText, expositionUniverselle.location)
durationLabel.text = String(format: NameSpace.durationText, expositionUniverselle.duration)
descriptionLabel.text = expositionUniverselle.description

 

 

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

 

String Format Specifiers

String Format Specifiers This article summarizes the format specifiers supported by string formatting methods and functions. Format SpecifiersThe format specifiers supported by the NSString formatting methods and CFString formatting functions follow the IE

developer.apple.com