質問編集履歴

1

解決できました。

2021/04/06 12:14

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -5,491 +5,3 @@
5
5
  インスタンスを作り、そのインスタンスのプロパティに追加した後、まとめてList型のプロパティに保存するしようとしたのですができません。
6
6
 
7
7
  なぜ保存されないのか理解できる方、どうかご教授お願いします。
8
-
9
-
10
-
11
- ### User
12
-
13
- ```ここに言語を入力
14
-
15
- class User: Object {
16
-
17
- @objc dynamic var id: Int = 0
18
-
19
- @objc dynamic var username: String = ""
20
-
21
- @objc dynamic var profileImage: Data? = nil
22
-
23
- var tweets = List<Tweet>()
24
-
25
- override static func primaryKey() -> String? {
26
-
27
- return "id"
28
-
29
- }
30
-
31
- }
32
-
33
-
34
-
35
- class Tweet: Object {
36
-
37
- @objc dynamic var tweetId: Int = 0
38
-
39
- @objc dynamic var caption: String = ""
40
-
41
- let users = LinkingObjects(fromType: User.self, property: "tweets")
42
-
43
- var replyTweet = List<ReplyTweet>()
44
-
45
- override static func primaryKey() -> String? {
46
-
47
- return "tweetId"
48
-
49
- }
50
-
51
- }
52
-
53
- ```
54
-
55
-
56
-
57
- ### データを追加するクラス
58
-
59
- ```ここに言語を入力
60
-
61
- import Foundation
62
-
63
- import UIKit
64
-
65
- import RealmSwift
66
-
67
-
68
-
69
- protocol CreateNewIDRepository {
70
-
71
- func newId<T: Object>(model: T) -> Int?
72
-
73
- func replyNewId<T: Object>(model: T) -> Int?
74
-
75
- }
76
-
77
-
78
-
79
- enum UploadTweetConfiguration {
80
-
81
- case tweet
82
-
83
- case reply(Tweet)
84
-
85
- }
86
-
87
-
88
-
89
- class UploadTweetController: UIViewController {
90
-
91
- private let currentUser = CurrentUser.shared
92
-
93
- private let reuseIdentifier = "UploadTweetCell"
94
-
95
- private let realm = try! Realm()
96
-
97
- private var user: Results<User>!
98
-
99
- private var tweets: [Tweet] = [Tweet]()
100
-
101
- private let config: UploadTweetConfiguration
102
-
103
- private let actionButtonTitle: String
104
-
105
- private let placeholderText: String
106
-
107
- private let shouldShowReplyLabel: Bool
108
-
109
- private var replyText: String?
110
-
111
- var textViewHeight: NSLayoutConstraint!
112
-
113
- private let imagePicker = UIImagePickerController()
114
-
115
- private var selectedImage = [UIImage]()
116
-
117
- private var collectionView: UICollectionView!
118
-
119
-
120
-
121
- private lazy var actionButton: UIButton = {
122
-
123
- let button = UIButton(type: .system)
124
-
125
- button.addTarget(self, action: #selector(handleUploadTweet), for: .touchUpInside)
126
-
127
- return button
128
-
129
- }()
130
-
131
-
132
-
133
- private let cameraButton = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(cameraButtonTapped))
134
-
135
-
136
-
137
- private let profileImageView: UIImageView = {
138
-
139
- let iv = UIImageView()
140
-
141
- return iv
142
-
143
- }()
144
-
145
-
146
-
147
- private let captionTextView = UITextView()
148
-
149
-
150
-
151
- private let placeholderLabel: UILabel = {
152
-
153
- let label = UILabel()
154
-
155
- return label
156
-
157
- }()
158
-
159
-
160
-
161
- private var documentDirectoryFileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
162
-
163
-
164
-
165
- private let filePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
166
-
167
-
168
-
169
- init(user: Results<User>!, config: UploadTweetConfiguration){
170
-
171
- self.user = user
172
-
173
- self.config = config
174
-
175
- switch config {
176
-
177
- case .tweet:
178
-
179
- actionButtonTitle = "Tweet"
180
-
181
- placeholderText = "What?"
182
-
183
- shouldShowReplyLabel = false
184
-
185
- case .reply(let tweet):
186
-
187
- actionButtonTitle = "reply"
188
-
189
- placeholderText = "Reply!"
190
-
191
- shouldShowReplyLabel = true
192
-
193
- }
194
-
195
- super.init(nibName: nil, bundle: nil)
196
-
197
- }
198
-
199
-
200
-
201
- required init?(coder: NSCoder) {
202
-
203
- fatalError("init(coder:) has not been implemented")
204
-
205
- }
206
-
207
-
208
-
209
- override func viewDidLoad() {
210
-
211
- super.viewDidLoad()
212
-
213
- captionTextView.delegate = self
214
-
215
- collectionView = UICollectionView(frame: view.frame, collectionViewLayout: UICollectionViewFlowLayout())
216
-
217
- collectionView.reloadData()
218
-
219
- configureUI()
220
-
221
- commonInit(button: true)
222
-
223
- configureImagePicker()
224
-
225
- NotificationCenter.default.addObserver(self,
226
-
227
- selector: #selector(textViewDidChange(notification:)),
228
-
229
- name: UITextView.textDidChangeNotification, object: captionTextView)
230
-
231
- }
232
-
233
-
234
-
235
- override func viewDidAppear(_ animated: Bool) {
236
-
237
- super.viewDidAppear(animated)
238
-
239
- collectionView.reloadData()
240
-
241
- }
242
-
243
-
244
-
245
- deinit {
246
-
247
- NotificationCenter.default.removeObserver(self)
248
-
249
- }
250
-
251
-
252
-
253
- @objc private func handleTextInputChange(){
254
-
255
- }
256
-
257
-
258
-
259
- @objc private func handleCancel(){
260
-
261
- dismiss(animated: true, completion: nil)
262
-
263
- }
264
-
265
-
266
-
267
- @objc private func handleUploadTweet(){
268
-
269
- view.endEditing(true)
270
-
271
- let userRealm = User()
272
-
273
- let tweetRealm = Tweet()
274
-
275
- let replyTweetRealm = ReplyTweet()
276
-
277
- guard let caption = captionTextView.text else { return }
278
-
279
- uploadTweet(caption: caption, type: config) { [self] in
280
-
281
- switch config {
282
-
283
- case .tweet: // ここ
284
-
285
- tweetRealm.caption = caption
286
-
287
- tweetRealm.tweetId = newId(model: tweetRealm)!
288
-
289
- userRealm.tweets.append(tweetRealm)
290
-
291
- try! realm.write {
292
-
293
- realm.add(tweetRealm, update: .all)
294
-
295
- realm.add(userRealm.tweets)
296
-
297
- }
298
-
299
- case .reply(let tweet):
300
-
301
- replyTweetRealm.replyCaption = caption
302
-
303
- replyTweetRealm.replyTweetId = replyNewId(model: replyTweetRealm)!
304
-
305
- try! realm.write {
306
-
307
- tweet.replyTweet.append(replyTweetRealm)
308
-
309
- }
310
-
311
- }
312
-
313
- }
314
-
315
- dismiss(animated: true, completion: nil)
316
-
317
- }
318
-
319
-
320
-
321
- @objc private func textViewDidChange(notification: NSNotification){
322
-
323
- }
324
-
325
-
326
-
327
- @objc private func cameraButtonTapped(){
328
-
329
- }
330
-
331
-
332
-
333
- // MARK: - UI
334
-
335
-
336
-
337
- private func configureUI(){
338
-
339
- }
340
-
341
-
342
-
343
- private func configureNavigationBar(){
344
-
345
- }
346
-
347
-
348
-
349
- private func uploadTweet(caption: String, type: UploadTweetConfiguration, completion: @escaping() -> Void){
350
-
351
- completion()
352
-
353
- }
354
-
355
-
356
-
357
- private func configureImagePicker(){
358
-
359
- imagePicker.delegate = self
360
-
361
- imagePicker.allowsEditing = true
362
-
363
- }
364
-
365
-
366
-
367
- private func getDocumentsURL() -> NSURL {
368
-
369
- let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
370
-
371
- return documentsURL
372
-
373
- }
374
-
375
-
376
-
377
- private func fileInDocumentsDirectory(filename: String) -> String {
378
-
379
- let fileURL = getDocumentsURL().appendingPathComponent(filename)
380
-
381
- return fileURL!.path
382
-
383
- }
384
-
385
-
386
-
387
- private func saveImageToDocuments(image: UIImage, path: String) {
388
-
389
- do {
390
-
391
- let pngImageData = image.pngData()
392
-
393
- try pngImageData!.write(to: URL(fileURLWithPath: path), options: .atomic)
394
-
395
- } catch {
396
-
397
- print(error)
398
-
399
- }
400
-
401
- }
402
-
403
-
404
-
405
- func saveImage() {
406
-
407
- }
408
-
409
-
410
-
411
- func imageNewId<T: Object>(model: T) -> Int? {
412
-
413
- }
414
-
415
-
416
-
417
- }
418
-
419
-
420
-
421
- extension UploadTweetController: CreateNewIDRepository {
422
-
423
-
424
-
425
- internal func newId<T: Object>(model: T) -> Int? {
426
-
427
- guard let key = T.primaryKey() else { return nil }
428
-
429
- if let last = realm.objects(T.self).sorted(byKeyPath: "tweetId", ascending: true).last,
430
-
431
- let lastId = last[key] as? Int {
432
-
433
- return lastId + 1
434
-
435
- } else {
436
-
437
- return 0
438
-
439
- }
440
-
441
- }
442
-
443
-
444
-
445
- internal func replyNewId<T: Object>(model: T) -> Int? {
446
-
447
- guard let key = T.primaryKey() else { return nil }
448
-
449
- if let last = realm.objects(T.self).sorted(byKeyPath: "replyTweetId", ascending: true).last,
450
-
451
- let lastId = last[key] as? Int {
452
-
453
- return lastId + 1
454
-
455
- } else {
456
-
457
- return 0
458
-
459
- }
460
-
461
- }
462
-
463
-
464
-
465
- }
466
-
467
-
468
-
469
- extension UploadTweetController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
470
-
471
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
472
-
473
- guard let image = info[.editedImage] as? UIImage else { return }
474
-
475
- selectedImage.append(image)
476
-
477
- collectionView.reloadData()
478
-
479
- dismiss(animated: true, completion: nil)
480
-
481
- }
482
-
483
- }
484
-
485
-
486
-
487
- extension UploadTweetController: UITextViewDelegate {
488
-
489
- func textViewDidChange(_ textView: UITextView) {
490
-
491
- }
492
-
493
- }
494
-
495
- ```