質問編集履歴

1

ソースコードとエラーメッセージの修正・追記を行いました。

2019/12/12 03:56

投稿

Nagi7
Nagi7

スコア5

test CHANGED
@@ -1 +1 @@
1
- Unexpectedly found nil while implicitly unwrapping an Optional valueのエラーを解決した
1
+ Unexpectedly found nil while implicitly unwrapping an Optional valueのエラーで困って
test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
  ```
34
34
 
35
- Unexpectedly found nil while implicitly unwrapping an Optional value
35
+ Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file /Users/apple/Desktop/Brio/Brio/Main Pages/Controller/RoomViewController.swift, line 293
36
36
 
37
37
  ```
38
38
 
@@ -48,138 +48,150 @@
48
48
 
49
49
  func loadTimeline() {
50
50
 
51
- guard let currentUser = NCMBUser.current() else {
51
+ guard let currentUser = NCMBUser.current() else {
52
-
52
+
53
- let storyboard = UIStoryboard(name: "SignIn", bundle: Bundle.main)
53
+ let storyboard = UIStoryboard(name: "SignIn", bundle: Bundle.main)
54
-
54
+
55
- let rootViewController = storyboard.instantiateViewController(withIdentifier:
55
+ let rootViewController = storyboard.instantiateViewController(withIdentifier:
56
-
56
+
57
- "RootNavigationController")
57
+ "RootNavigationController")
58
-
58
+
59
- UIApplication.shared.keyWindow?.rootViewController = rootViewController
59
+ UIApplication.shared.keyWindow?.rootViewController = rootViewController
60
60
 
61
61
 
62
62
 
63
- let ud = UserDefaults.standard
63
+ let ud = UserDefaults.standard
64
-
64
+
65
- ud.set(false, forKey: "isLogin")
65
+ ud.set(false, forKey: "isLogin")
66
-
66
+
67
- ud.synchronize()
67
+ ud.synchronize()
68
-
68
+
69
- return
69
+ return
70
-
70
+
71
- }
71
+ }
72
-
73
-
74
-
72
+
73
+
74
+
75
- let query = NCMBQuery(className: "Post")
75
+ let query = NCMBQuery(className: "Post")
76
+
77
+
78
+
76
-
79
+ // 降順
77
-
78
-
80
+
79
- query?.order(byDescending: "createDate")
81
+ query?.order(byDescending: "createDate")
82
+
83
+
84
+
80
-
85
+ // 投稿したユーザーの情報も同時取得
81
-
82
-
83
-
84
-
86
+
85
- query?.includeKey("user")
87
+ query?.includeKey("user")
88
+
89
+
90
+
86
-
91
+ // オブジェクトの取得
87
-
88
-
92
+
89
- query?.findObjectsInBackground({ (result, error) in
93
+ query?.findObjectsInBackground({ (result, error) in
90
-
94
+
91
- if error != nil {
95
+ if error != nil {
92
-
96
+
93
- SVProgressHUD.showError(withStatus: error!.localizedDescription)
97
+ SVProgressHUD.showError(withStatus: error!.localizedDescription)
94
-
98
+
95
- } else {
99
+ } else {
96
-
97
- self.posts = [Post]()
98
100
 
99
101
 
100
102
 
103
+ // 投稿を格納しておく配列を初期化
104
+
105
+ self.posts = [Post]()
106
+
107
+
108
+
101
- for postObject in result as! [NCMBObject] {
109
+ for postObject in result as! [NCMBObject] {
102
110
 
103
111
 
104
112
 
105
-
113
+ // ユーザー情報をUserクラスにセット
106
-
114
+
107
- let user = postObject.object(forKey: "user") as! NCMBUser
115
+ let user = postObject.object(forKey: "user") as! NCMBUser
108
116
 
109
117
 
110
118
 
111
-
112
-
113
- if user.object(forKey: "active") as? Bool != false {
114
-
115
-
116
-
117
- // 投稿したユーザーの情報をUserモデルにまとめる
118
-
119
- let userModel = User(objectId: user.objectId, userName: user.userName)
120
-
121
- userModel.displayName = user.object(forKey: "displayName") as? String
122
-
123
-
124
-
125
- // 投稿の情報を取得
126
-
127
- let imageUrl = user.object(forKey: "imageUrl") as! String
128
-
129
-
130
-
131
- let text = postObject.object(forKey: "text") as! String
132
-
133
-
134
-
135
- let post = Post(objectId: postObject.objectId, user: userModel, imageUrl: imageUrl, text: text, createDate: postObject.createDate, roomId: "")
136
-
137
-
138
-
139
-
140
-
141
- let likeUsers = postObject.object(forKey: "likeUser") as? [String]
142
-
143
- if likeUsers?.contains(currentUser.objectId) == true {
144
-
145
- post.isLiked = true
146
-
147
- } else {
148
-
149
- post.isLiked = false
150
-
151
- }
152
-
153
-
154
-
155
-
156
-
157
- if let likes = likeUsers {
158
-
159
- post.likeCount = likes.count
160
-
161
- }
162
-
163
-
164
-
165
-
166
-
167
- self.posts.append(post)
168
-
169
- }
170
-
171
- }
172
-
173
-
174
-
175
- self.RoomTableView.reloadData()
119
+ // 退会済みユーザーの投稿を避けるため、activeがfalse以外のモノだけを表示
120
+
121
+ if user.object(forKey: "active") as? Bool != false {
122
+
123
+
124
+
125
+ // 投稿したユーザーの情報をUserモデルにまとめる
126
+
127
+ let userModel = User(objectId: user.objectId, userName: user.userName)
128
+
129
+ userModel.displayName = user.object(forKey: "displayName") as? String
130
+
131
+
132
+
133
+ // 投稿の情報を取得
134
+
135
+ let imageUrl = user.object(forKey: "imageUrl") as! String
136
+
137
+
138
+
139
+ let text = postObject.object(forKey: "text") as! String
140
+
141
+
142
+
143
+ // 2つのデータ(投稿情報と誰が投稿したか?)を合わせてPostクラスにセット
144
+
145
+ let post = Post(objectId: postObject.objectId, user: userModel, imageUrl: imageUrl, text: text, createDate: postObject.createDate, roomId: "")
146
+
147
+
148
+
149
+ // likeの状況(自分が過去にLikeしているか?)によってデータを挿入
150
+
151
+ let likeUsers = postObject.object(forKey: "likeUser") as? [String]
152
+
153
+ if likeUsers?.contains(currentUser.objectId) == true {
154
+
155
+ post.isLiked = true
156
+
157
+ } else {
158
+
159
+ post.isLiked = false
160
+
161
+ }
162
+
163
+
164
+
165
+ // いいねの件数
166
+
167
+ if let likes = likeUsers {
168
+
169
+ post.likeCount = likes.count
170
+
171
+ }
172
+
173
+
174
+
175
+ // 配列に加える
176
+
177
+ self.posts.append(post)
178
+
179
+ }
180
+
181
+ }
182
+
183
+
184
+
185
+ // 投稿のデータが揃ったらTableViewをリロード
186
+
187
+ self.RoomTableView.reloadData()
188
+
189
+ }
190
+
191
+ })
176
192
 
177
193
  }
178
194
 
179
- })
180
-
181
- }
182
-
183
195
 
184
196
 
185
197