0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

他で使用予定のないメソッドをprivate extensionにして分ける

Posted at

リファクタリングの一環で使えそうなことを、メモがてら残します。

let alertVC = UIAlertController(title: "select cell", message: "\(user["name"] as! String) \n \(user["atName"] as! String)", preferredStyle: .alert)
    alertVC.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    self.present(alertVC, animated: true, completion: nil)

問題点

  • 他で使用予定が無いのに呼び出し可能
  • 色々な条件でアラートを出したい場合、汎用性が無い

private extensionにして分ける

private extension ViewController {
  //アラートを出す処理
  func showAlert(user: UserModel) { // UserModelというモデルがあると仮定
    let alertVC = UIAlertController(title: "select cell", message: user.alertText, preferredStyle: .alert)
    alertVC.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    self.present(alertVC, animated: true, completion: nil)
  }
}
  • private extensionにすれば、中の記述は全てprivate
  • showAlert(user: user) のような形で呼び出し可能で、汎用性が増す
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?