From 0fcd865875d2e55f07227edfaeaffce368d7bcde Mon Sep 17 00:00:00 2001 From: Kai Oishi Date: Sun, 25 Apr 2021 09:38:11 +0900 Subject: [PATCH] =?UTF-8?q?=E5=91=BD=E5=90=8D=E3=81=97=E7=9B=B4=E3=81=97#3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DetailViewController.swift | 28 +++++++++---------- .../SearchViewController.swift | 21 +++++++------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/iOSEngineerCodeCheck/DetailViewController.swift b/iOSEngineerCodeCheck/DetailViewController.swift index 748e7d8..64a0220 100644 --- a/iOSEngineerCodeCheck/DetailViewController.swift +++ b/iOSEngineerCodeCheck/DetailViewController.swift @@ -21,33 +21,33 @@ class DetailViewController: UIViewController { @IBOutlet weak var forksLabel: UILabel! @IBOutlet weak var issuesLabel: UILabel! - var vc1: SearchViewController! + var searchViewController: SearchViewController! override func viewDidLoad() { super.viewDidLoad() - let repo = vc1.repositories[vc1.idx] + let repository = searchViewController.repositories[searchViewController.selectedIndex] - languageLabel.text = "Written in \(repo["language"] as? String ?? "")" - starsLabel.text = "\(repo["stargazers_count"] as? Int ?? 0) stars" - watcherLabel.text = "\(repo["wachers_count"] as? Int ?? 0) watchers" - forksLabel.text = "\(repo["forks_count"] as? Int ?? 0) forks" - issuesLabel.text = "\(repo["open_issues_count"] as? Int ?? 0) open issues" + languageLabel.text = "Written in \(repository["language"] as? String ?? "")" + starsLabel.text = "\(repository["stargazers_count"] as? Int ?? 0) stars" + watcherLabel.text = "\(repository["wachers_count"] as? Int ?? 0) watchers" + forksLabel.text = "\(repository["forks_count"] as? Int ?? 0) forks" + issuesLabel.text = "\(repository["open_issues_count"] as? Int ?? 0) open issues" getImage() } func getImage(){ - let repo = vc1.repositories[vc1.idx] + let repository = searchViewController.repositories[searchViewController.selectedIndex] - titleLabel.text = repo["full_name"] as? String + titleLabel.text = repository["full_name"] as? String - if let owner = repo["owner"] as? [String: Any] { - if let imgURL = owner["avatar_url"] as? String { - URLSession.shared.dataTask(with: URL(string: imgURL)!) { (data, res, err) in - let img = UIImage(data: data!)! + if let owner = repository["owner"] as? [String: Any] { + if let avatarURL = owner["avatar_url"] as? String { + URLSession.shared.dataTask(with: URL(string: avatarURL)!) { (data, res, err) in + let image = UIImage(data: data!)! DispatchQueue.main.async { - self.imageView.image = img + self.imageView.image = image } }.resume() } diff --git a/iOSEngineerCodeCheck/SearchViewController.swift b/iOSEngineerCodeCheck/SearchViewController.swift index abf752b..ad3d597 100644 --- a/iOSEngineerCodeCheck/SearchViewController.swift +++ b/iOSEngineerCodeCheck/SearchViewController.swift @@ -16,8 +16,7 @@ class SearchViewController: UITableViewController, UISearchBarDelegate { var task: URLSessionTask? var word: String! - var url: String! - var idx: Int! + var selectedIndex: Int! override func viewDidLoad() { super.viewDidLoad() @@ -38,10 +37,10 @@ class SearchViewController: UITableViewController, UISearchBarDelegate { word = searchBar.text! if word.count != 0 { - url = "https://api.github.com/search/repositories?q=\(word!)" + let url = "https://api.github.com/search/repositories?q=\(word!)" task = URLSession.shared.dataTask(with: URL(string: url)!) { (data, res, err) in - if let obj = try! JSONSerialization.jsonObject(with: data!) as? [String: Any] { - if let items = obj["items"] as? [[String: Any]] { + if let objects = try! JSONSerialization.jsonObject(with: data!) as? [String: Any] { + if let items = objects["items"] as? [[String: Any]] { self.repositories = items DispatchQueue.main.async { self.tableView.reloadData() @@ -55,8 +54,8 @@ class SearchViewController: UITableViewController, UISearchBarDelegate { override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "Detail" { - let dtl = segue.destination as! DetailViewController - dtl.vc1 = self + let detailViewController = segue.destination as! DetailViewController + detailViewController.searchViewController = self } } @@ -66,15 +65,15 @@ class SearchViewController: UITableViewController, UISearchBarDelegate { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() - let rp = repositories[indexPath.row] - cell.textLabel?.text = rp["full_name"] as? String ?? "" - cell.detailTextLabel?.text = rp["language"] as? String ?? "" + let repository = repositories[indexPath.row] + cell.textLabel?.text = repository["full_name"] as? String ?? "" + cell.detailTextLabel?.text = repository["language"] as? String ?? "" cell.tag = indexPath.row return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - idx = indexPath.row + selectedIndex = indexPath.row performSegue(withIdentifier: "Detail", sender: self) } }