puuurm / swift-weatherforecast

iOS app
1 stars 0 forks source link

Delete string literal: UIImage #39

Closed puuurm closed 6 years ago

puuurm commented 6 years ago

최대한 스트링 리터럴을 쓰지 않도록 리팩토링 한다. https://cocoacasts.com/the-danger-of-string-literals-and-stringly-typed-code

extension UIImage {

    enum Icons {

        enum Weather {

            // 원래 강제 옵셔널 추출은 스위프트에서 좋지 않은 표현이지만 이미지 asset을 가져오는 경우는 예외라고 한다.
            static let Pressure = UIImage(named: "pressure")! 
            static let Humidity = UIImage(named: "humidity")!
            static let Wind = UIImage(named: "wind")!
            static let Clouds = UIImage(named: "clouds")!
            static let Rain = UIImage(named: "rain")!
            static let Snow = UIImage(named: "snow")!

        }
}
puuurm commented 6 years ago

I rarely use Swift's exclamation mark. One exception is the loading of assets and resources from the application's bundle. Such an operation should never fail. If it fails, then I have other things to worry about and the application crashing due to a runtime error is the least of my worries. 출처: https://cocoacasts.com/the-danger-of-string-literals-and-stringly-typed-code