質問編集履歴

3

a

2020/09/10 03:02

投稿

kazuki_user
kazuki_user

スコア147

test CHANGED
File without changes
test CHANGED
@@ -48,11 +48,55 @@
48
48
 
49
49
 
50
50
 
51
+ import UIKit
52
+
53
+
54
+
55
+ let URL_BASE = "https://api.github.com/search/repositories?q="
56
+
51
- var selectedUser: SearchRootVC!
57
+ var selectedUser = SearchRootVC()
52
-
53
- // 以下letでエラー。
58
+
54
-
55
- let repo = selectedUser.repo[selectedUser.RepoToPass] // Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
59
+ let repo = selectedUser.repo[selectedUser.RepoToPass] // Unexpectedly found nil while implicitly unwrapping an Optional value
60
+
61
+
62
+
63
+ struct Segues {
64
+
65
+ // static let ... 自動的に遅延初期化される(初回アクセスのタイミングでインスタンス生成)
66
+
67
+ static let ToProfileDetail = "Detail"
68
+
69
+ }
70
+
71
+
72
+
73
+ struct Identifiers {
74
+
75
+ static let RepositoryCell = "Repository"
76
+
77
+ }
78
+
79
+
80
+
81
+ struct ApiKey {
82
+
83
+ static let Language = "language"
84
+
85
+ static let Stars = "stargazers_count"
86
+
87
+ static let Watchers = "wachers_count"
88
+
89
+ static let Forks = "forks_count"
90
+
91
+ static let Issues = "open_issues_count"
92
+
93
+ static let FullName = "full_name"
94
+
95
+ static let User = "owner"
96
+
97
+ static let imgUrl = "avatar_url"
98
+
99
+ }
56
100
 
57
101
 
58
102
 
@@ -70,14 +114,90 @@
70
114
 
71
115
  }
72
116
 
117
+
118
+
73
119
  ```
74
120
 
75
121
 
76
122
 
77
123
  ```ProfileDetailVC
78
124
 
125
+ import UIKit
126
+
127
+
128
+
129
+ class ProfileDetailVC: UIViewController {
130
+
131
+
132
+
133
+ @IBOutlet weak var ImgView: UIImageView!
134
+
135
+
136
+
137
+ @IBOutlet weak var TitleLbl: UILabel!
138
+
139
+
140
+
141
+ @IBOutlet weak var LanguageLbl: UILabel!
142
+
143
+
144
+
145
+ @IBOutlet weak var StarsLbl: UILabel!
146
+
147
+ @IBOutlet weak var WatchersLbl: UILabel!
148
+
149
+ @IBOutlet weak var ForksLbl: UILabel!
150
+
151
+ @IBOutlet weak var IssuesLbl: UILabel!
152
+
153
+
154
+
155
+ // Var
156
+
157
+ var selectedUser: SearchRootVC!
158
+
159
+
160
+
161
+ override func viewDidLoad() {
162
+
163
+ super.viewDidLoad()
164
+
165
+ setupUI() // Repository型の変数を渡す
166
+
167
+ getProfileImage()
168
+
169
+ }
170
+
171
+
172
+
173
+ // func setupUI() {
174
+
175
+ // TitleLbl.text = repo[ApiKey.FullName] as? String
176
+
177
+ // LanguageLbl.text = repoTxt.language
178
+
179
+ // StarsLbl.text = repoTxt.stars
180
+
181
+ // WatchersLbl.text = repoTxt.watchers
182
+
183
+ // ForksLbl.text = repoTxt.forks
184
+
185
+ // IssuesLbl.text = repoTxt.issues
186
+
187
+ // }
188
+
189
+
190
+
79
191
  func setupUI() {
80
192
 
193
+
194
+
195
+ let repo = selectedUser.repo[selectedUser.RepoToPass]
196
+
197
+
198
+
199
+ TitleLbl.text = repo[ApiKey.FullName] as? String
200
+
81
201
  LanguageLbl.text = repo[repoTxt.language] as? String
82
202
 
83
203
  StarsLbl.text = repo[repoTxt.stars] as? String
@@ -90,6 +210,62 @@
90
210
 
91
211
  }
92
212
 
213
+
214
+
215
+ func getProfileImage() {
216
+
217
+
218
+
219
+ let repo = selectedUser.repo[selectedUser.RepoToPass]
220
+
221
+
222
+
223
+ guard let user = repo[ApiKey.User] as? [String: Any],
224
+
225
+ let imgURL = user[ApiKey.imgUrl] as? String,
226
+
227
+ let url = URL(string: imgURL)
228
+
229
+ else { return }
230
+
231
+
232
+
233
+ let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
234
+
235
+
236
+
237
+ guard error == nil else {
238
+
239
+ debugPrint(error.debugDescription)
240
+
241
+ return
242
+
243
+ }
244
+
245
+
246
+
247
+ guard let data = data else { return }
248
+
249
+
250
+
251
+ let img = UIImage(data: data)
252
+
253
+ DispatchQueue.main.async {
254
+
255
+ self.ImgView.image = img
256
+
257
+ }
258
+
259
+ }
260
+
261
+ task.resume()
262
+
263
+ }
264
+
265
+ }
266
+
267
+
268
+
93
269
  ```
94
270
 
95
271
 

2

a

2020/09/10 03:02

投稿

kazuki_user
kazuki_user

スコア147

test CHANGED
File without changes
test CHANGED
@@ -78,15 +78,15 @@
78
78
 
79
79
  func setupUI() {
80
80
 
81
- LanguageLbl.text = repoTxt.language
81
+ LanguageLbl.text = repo[repoTxt.language] as? String
82
82
 
83
- StarsLbl.text = repoTxt.stars
83
+ StarsLbl.text = repo[repoTxt.stars] as? String
84
84
 
85
- WatchersLbl.text = repoTxt.watchers
85
+ WatchersLbl.text = repo[repoTxt.watchers] as? String
86
86
 
87
- ForksLbl.text = repoTxt.forks
87
+ ForksLbl.text = repo[repoTxt.forks] as? String
88
88
 
89
- IssuesLbl.text = repoTxt.issues
89
+ IssuesLbl.text = repo[repoTxt.issues] as? String
90
90
 
91
91
  }
92
92
 

1

a

2020/09/10 02:47

投稿

kazuki_user
kazuki_user

スコア147

test CHANGED
@@ -1 +1 @@
1
- 画面遷移するとき、Unexpectedly found nilエラー
1
+ selectedUserがnilで、Unexpectedly found nilエラー
test CHANGED
@@ -1,6 +1,8 @@
1
1
  ## 画面遷移するとき、Unexpectedly found nilエラー。
2
2
 
3
3
 
4
+
5
+ - `selectedUser`が**nil**
4
6
 
5
7
  - テキストデータを、Modelに格納して、`ProfileDetailVC`で表示したい。
6
8