質問編集履歴

1

基礎を見直すと解決できます。

2021/04/07 01:10

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,463 +1,3 @@
1
1
  Realmの初期データを作り、最初はプロフィール画像がないためアセットの画像を表示し、プロフィール設定した後には画像をUIImageに変換し表示しようと考えました。ビルドすると何もエラーは出てこないのですが、いざ実行すると、アンラップしろと出てきました。UserクラスではprofileImageはnilとし、Objectを継承するとオプショナルなのでアンラップする必要はないはずです。
2
2
 
3
3
  なんだか矛盾しているのでよく分からない状態でございます。
4
-
5
-
6
-
7
- それとも、コードでどこか間違えているのかもしれないので、どなたかご教授してくださると助かります。
8
-
9
- 下に、Userクラスと、コード全体を掲載しておきます。
10
-
11
-
12
-
13
- それではよろしくお願いします。
14
-
15
-
16
-
17
- ![イメージ説明](ddbd5db7974e1295fa42e888a2de07bd.jpeg)
18
-
19
- ```ここに言語を入力
20
-
21
- class User: Object {
22
-
23
-
24
-
25
- @objc dynamic var id: Int = 0
26
-
27
- @objc dynamic var fullname: String = ""
28
-
29
- @objc dynamic var username: String = ""
30
-
31
- @objc dynamic var profileText: String = ""
32
-
33
- @objc dynamic var profileImage: Data? = nil
34
-
35
-
36
-
37
- override static func primaryKey() -> String? {
38
-
39
- return "id"
40
-
41
- }
42
-
43
-
44
-
45
- }
46
-
47
-
48
-
49
- ```
50
-
51
-
52
-
53
- ```ここに言語を入力
54
-
55
- // FeedController.swift
56
-
57
-
58
-
59
- import UIKit
60
-
61
- import RealmSwift
62
-
63
-
64
-
65
- private let reuseIdentifier = "TweetCell"
66
-
67
-
68
-
69
- class FeedController: UICollectionViewController {
70
-
71
-
72
-
73
- //MARK: - Properties
74
-
75
-
76
-
77
- var userItems: Results<User>! {
78
-
79
- didSet { configureLeftBarButton() }
80
-
81
- }
82
-
83
-
84
-
85
- private var user: User!
86
-
87
- private var profileImage: UIImage?
88
-
89
-
90
-
91
- private var tweets = [Tweet]() {
92
-
93
- didSet { collectionView.reloadData() }
94
-
95
- }
96
-
97
-
98
-
99
- //MARK: - LifeCycle
100
-
101
-
102
-
103
- override func viewDidLoad() {
104
-
105
- super.viewDidLoad()
106
-
107
-
108
-
109
- configureUI()
110
-
111
- // setRealm()
112
-
113
- // fetchTweets()
114
-
115
- }
116
-
117
-
118
-
119
- override func viewWillAppear(_ animated: Bool) {
120
-
121
- super.viewWillAppear(animated)
122
-
123
- navigationController?.navigationBar.barStyle = .default
124
-
125
- navigationController?.navigationBar.isHidden = false
126
-
127
- }
128
-
129
-
130
-
131
- // MARK: - API
132
-
133
-
134
-
135
- func fetchTweets(){
136
-
137
- // ここでUploadTweetControllerからデータをもらう
138
-
139
-
140
-
141
- // self.tweets = tweets
142
-
143
- }
144
-
145
-
146
-
147
- // MARK: - Selectors
148
-
149
-
150
-
151
- @objc func handleRefresh(){
152
-
153
- fetchTweets()
154
-
155
- }
156
-
157
-
158
-
159
- @objc func handleProfileImageTap() {
160
-
161
- guard let user = user else { return }
162
-
163
- let controller = ProfileController(user: user)
164
-
165
- navigationController?.pushViewController(controller, animated: true)
166
-
167
- }
168
-
169
-
170
-
171
- //MARK: - Helpers
172
-
173
-
174
-
175
- func configureUI(){
176
-
177
- view.backgroundColor = .white
178
-
179
-
180
-
181
- collectionView.register(TweetCell.self, forCellWithReuseIdentifier: reuseIdentifier)
182
-
183
- collectionView.backgroundColor = .white
184
-
185
-
186
-
187
- let imageView = UIImageView(image: UIImage(named: "Twitter_Logo_Blue"))
188
-
189
- imageView.contentMode = .scaleAspectFill
190
-
191
- imageView.setDimensions(width: 44, height: 44)
192
-
193
- navigationItem.titleView = imageView
194
-
195
- print(Realm.Configuration.defaultConfiguration.fileURL!)
196
-
197
- configureLeftBarButton()
198
-
199
- }
200
-
201
-
202
-
203
- func configureLeftBarButton(){
204
-
205
-
206
-
207
- if user.profileImage != nil {
208
-
209
- let pngData = profileImage?.toPNGData()
210
-
211
- let jpegData = profileImage?.toJPEGData()
212
-
213
- let profileImageView = UIImageView()
214
-
215
- // user.fullname = fullnameTextField.text!
216
-
217
- // user.username = usernameTextField.text!
218
-
219
- user.profileImage = pngData ?? jpegData
220
-
221
- profileImageView.image = UIImage(data: user!.profileImage!)
222
-
223
- profileImageView.setDimensions(width: 32, height: 32)
224
-
225
- profileImageView.layer.cornerRadius = 32 / 2
226
-
227
- profileImageView.layer.masksToBounds = true
228
-
229
- let tap = UITapGestureRecognizer(target: self, action: #selector(handleProfileImageTap))
230
-
231
- profileImageView.addGestureRecognizer(tap)
232
-
233
- navigationItem.leftBarButtonItem = UIBarButtonItem(customView: profileImageView)
234
-
235
- } else {
236
-
237
- let profileImageView = UIImageView()
238
-
239
- profileImageView.image = UIImage(named: "placeholderImg")
240
-
241
- profileImageView.setDimensions(width: 32, height: 32)
242
-
243
- profileImageView.layer.cornerRadius = 32 / 2
244
-
245
- profileImageView.layer.masksToBounds = true
246
-
247
- let tap = UITapGestureRecognizer(target: self, action: #selector(handleProfileImageTap))
248
-
249
- profileImageView.addGestureRecognizer(tap)
250
-
251
- navigationItem.leftBarButtonItem = UIBarButtonItem(customView: profileImageView)
252
-
253
- }
254
-
255
-
256
-
257
- }
258
-
259
-
260
-
261
- private func setRealm() {
262
-
263
- let realm = try! Realm()
264
-
265
- userItems = realm.objects(User.self)
266
-
267
- let initUserData: Results<User> = realm.objects(User.self)
268
-
269
- print("initUserData: (initUserData)")
270
-
271
- }
272
-
273
-
274
-
275
- func configureUserCell(image: UIImage, userName: String, fullName: String){
276
-
277
- let realm = try! Realm()
278
-
279
- userItems = realm.objects(User.self)
280
-
281
- // profileImageView.image = image
282
-
283
- }
284
-
285
-
286
-
287
- func newId<T: Object>(model: T) -> Int? {
288
-
289
- guard let key = T.primaryKey() else { return nil }
290
-
291
- let realm = try! Realm()
292
-
293
- if let last = realm.objects(T.self).sorted(byKeyPath: "id", ascending: true).last,
294
-
295
- let lastId = last[key] as? Int {
296
-
297
- return lastId + 1
298
-
299
- } else {
300
-
301
- return 0
302
-
303
- }
304
-
305
- }
306
-
307
-
308
-
309
- }
310
-
311
-
312
-
313
- // MARK: - UICollectionViewDelegate/DataSource
314
-
315
-
316
-
317
- extension FeedController {
318
-
319
-
320
-
321
- override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
322
-
323
- // return tweets.count
324
-
325
- return 1
326
-
327
- }
328
-
329
-
330
-
331
- override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
332
-
333
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
334
-
335
-
336
-
337
- // cell.tweet = tweets[indexPath.row]
338
-
339
- return cell
340
-
341
- }
342
-
343
-
344
-
345
- override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
346
-
347
- // let controller = TweetController(tweet: tweets[indexPath.row])
348
-
349
- // let controller = ProfileController(user: user!)
350
-
351
- // navigationController?.pushViewController(controller, animated: true)
352
-
353
- }
354
-
355
-
356
-
357
- }
358
-
359
-
360
-
361
- // MARK: - UICollectionViewDelegateFlowLayout
362
-
363
-
364
-
365
- extension FeedController: UICollectionViewDelegateFlowLayout {
366
-
367
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
368
-
369
- // let viewModel = TweetViewModel(tweet: tweets[indexPath.row])
370
-
371
- // let height = viewModel.size(forWidth: view.frame.width).height
372
-
373
- // return CGSize(width: view.frame.width, height: height + 72)
374
-
375
- return CGSize(width: view.frame.width, height: 100)
376
-
377
- }
378
-
379
- }
380
-
381
-
382
-
383
- // MARK: - TweetCellDelegate
384
-
385
-
386
-
387
- extension FeedController: TweetCellDelegate {
388
-
389
-
390
-
391
- func handleProfileImageTapped(_ cell: TweetCell) {
392
-
393
- // guard let user = cell.tweet?.user else { return }
394
-
395
- let controller = ProfileController(user: user)
396
-
397
- navigationController?.pushViewController(controller, animated: true)
398
-
399
- }
400
-
401
-
402
-
403
- }
404
-
405
-
406
-
407
- public extension UIImage {
408
-
409
-
410
-
411
- // MARK: Public Methods
412
-
413
-
414
-
415
- /// イメージ→PNGデータに変換する
416
-
417
- /// - Returns: 変換後のPNGデータ
418
-
419
- func toPNGData() -> Data {
420
-
421
- guard let data = self.pngData() else {
422
-
423
- print("イメージをPNGデータに変換できませんでした。")
424
-
425
- return Data()
426
-
427
- }
428
-
429
-
430
-
431
- return data as Data
432
-
433
- }
434
-
435
-
436
-
437
- /// イメージ→JPEGデータに変換する
438
-
439
- /// - Returns: 変換後のJPEGデータ
440
-
441
- func toJPEGData() -> Data {
442
-
443
- guard let data = self.jpegData(compressionQuality: 1.0) else {
444
-
445
- print("イメージをJPEGデータに変換できませんでした。")
446
-
447
- return Data()
448
-
449
- }
450
-
451
-
452
-
453
- return data as Data
454
-
455
- }
456
-
457
-
458
-
459
- }
460
-
461
-
462
-
463
- ```