レビューしてみます
Opened this issue · 0 comments
たまたま見つけたのででしゃばります
-
基本的にグローバルな変数・関数は定義しないことが多いです。以下の行だと特に、APIのURLなどなので、クラス・構造体内に定数として持たせたりすると良さそう。
https://github.com/tiking76/RxweatherApp/blob/master/Rxweatherapp/APIFormat.swift#L12 -
Implicitly Unwrapped Optionals
は特段理由がなければ使わないようにする方が良いです。理由があるものとして、APIのエンドポイントとかをURL(string:)
で初期化した時の Optional とかは使ったりすることがあります。
https://github.com/tiking76/RxweatherApp/blob/master/Rxweatherapp/APIFormat.swift#L13 -
Encode しないのなら
Decodable
に準拠するだけでも良いです
https://github.com/tiking76/RxweatherApp/blob/master/Rxweatherapp/APIFormat.swift#L17 -
こんな書き方もできるよってやつ
let text = "\(baseURL)\(location),jp&units=metric&APPID=\(myAPIKey)"
https://github.com/tiking76/RxweatherApp/blob/master/Rxweatherapp/APIFormat.swift#L32
- クロージャ による循環参照を防ぎましょう
クロージャ 内でself
を強参照してしまうと循環参照してしまいます。
今回の場合は循環参照してもメモリリークしたように見えない画面ですが、忘れないように毎回書いておくのおすすめです。こんな感じ
tokyoButton.rx.tap
.subscribe(onNext: { [weak self] in
guard let self = self else { return }
location = LocationData.tokyo.rawValue
self.client.getAddress()
self.weatherView.image = UIImage(named: self.client.weathericon)
self.view.addSubview(self.weatherView)
})
.disposed(by: disposedBeg)
https://github.com/tiking76/RxweatherApp/blob/master/Rxweatherapp/ViewController.swift#L29
-
key がそのまま値となるなら書かなくても良いよってやつ
enum
を使う時にString
を継承したりしている状態で、="hoge"
など書いていない場合、key の値がそのまま値となります。
Int
を継承した場合は0
から連番で値が設定されたりします。
https://github.com/tiking76/RxweatherApp/blob/master/Rxweatherapp/LocationData.swift#L12 -
getter のみを外部に公開すると良いかも?
以下みたいにやると、getter のみ外部に公開できます
private(set) var weathericon: String = ""
https://github.com/tiking76/RxweatherApp/blob/master/Rxweatherapp/APIFormat.swift#L29