質問編集履歴

1

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

2021/04/07 01:15

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,489 +1 @@
1
1
  Firebaseで作られたものを改良して、Realmでメモアプリを作りたく、デフォルト値を入れ、全体に反映させたいのですが、どこにどのタイミングで書けばいいのか分かりません。
2
-
3
- ```ここに言語を入力
4
-
5
- class User: Object {
6
-
7
-
8
-
9
- @objc dynamic var id: Int = 0
10
-
11
- @objc dynamic var fullname: String = "fullname"
12
-
13
- @objc dynamic var username: String = "anonymous"
14
-
15
- @objc dynamic var profileText: String = "自己紹介をお願いします。"
16
-
17
- @objc dynamic var profileImage: Data? = nil
18
-
19
-
20
-
21
- override static func primaryKey() -> String? {
22
-
23
- return "id"
24
-
25
- }
26
-
27
-
28
-
29
- }
30
-
31
- ```
32
-
33
- 例えば、初期起動の時にプロフィール編集画面にて、すでにデフォルト値が設定されていたいのですが、userを渡しても、何も反応がありません。
34
-
35
- ```ここに言語を入力
36
-
37
- import UIKit
38
-
39
- import RealmSwift
40
-
41
-
42
-
43
- private let reuseIdentifier = "EditProfileCell"
44
-
45
-
46
-
47
- protocol EditProfileControllerDelegate: class {
48
-
49
- func controller(_ controller: EditProfileController, wantsToUpdate user: User)
50
-
51
- func handleLogout()
52
-
53
- }
54
-
55
-
56
-
57
- class EditProfileController: UITableViewController {
58
-
59
-
60
-
61
- // MARK: - Properties
62
-
63
-
64
-
65
- private var user: User
66
-
67
- private lazy var headerView = EditProfileHeader(user: user)
68
-
69
- private let footerView = EditProfileFooter()
70
-
71
- private let imagePicker = UIImagePickerController()
72
-
73
- private var userInfoChange = false
74
-
75
-
76
-
77
- private var imageChanged: Bool {
78
-
79
- return selectedImage != nil
80
-
81
- }
82
-
83
-
84
-
85
- weak var delegate: EditProfileControllerDelegate?
86
-
87
-
88
-
89
- private var selectedImage: UIImage? {
90
-
91
- didSet {
92
-
93
- headerView.profileImageView.image = selectedImage
94
-
95
- }
96
-
97
- }
98
-
99
-
100
-
101
- // MARK: - Lifecycle
102
-
103
-
104
-
105
- init(user: User){
106
-
107
- self.user = user
108
-
109
- super.init(style: .plain)
110
-
111
- }
112
-
113
-
114
-
115
- required init?(coder: NSCoder) {
116
-
117
- fatalError("init(coder:) has not been implemented")
118
-
119
- }
120
-
121
-
122
-
123
- override func viewDidLoad() {
124
-
125
- super.viewDidLoad()
126
-
127
- configureNavigationBar()
128
-
129
- configureTableView()
130
-
131
- configureImagePicker()
132
-
133
- }
134
-
135
-
136
-
137
- // MARK: - Selectors
138
-
139
-
140
-
141
- @objc func handleCancel(){
142
-
143
- dismiss(animated: true, completion: nil)
144
-
145
- }
146
-
147
-
148
-
149
- @objc func handleDone(){
150
-
151
- view.endEditing(true)
152
-
153
- guard imageChanged || userInfoChange else { return }
154
-
155
- updateUserData()
156
-
157
- }
158
-
159
-
160
-
161
- // MARK: - API
162
-
163
-
164
-
165
- func updateUserData() {
166
-
167
-
168
-
169
- if imageChanged && !userInfoChange {
170
-
171
- print("DEBUG: Changed image and not data")
172
-
173
- updateProfileImage()
174
-
175
- }
176
-
177
-
178
-
179
- if userInfoChange && !imageChanged {
180
-
181
- // UserService.shared.saveUserData(user: user) { (error, ref) in
182
-
183
- // print("DEBUG: Changed data and not image...")
184
-
185
- // self.delegate?.controller(self, wantsToUpdate: self.user)
186
-
187
- // }
188
-
189
- }
190
-
191
-
192
-
193
- if userInfoChange && imageChanged {
194
-
195
- print("DEBUG: Changed both...")
196
-
197
- // UserService.shared.saveUserData(user: user) { (error, ref) in
198
-
199
- // self.updateProfileImage()
200
-
201
- // }
202
-
203
- }
204
-
205
-
206
-
207
- }
208
-
209
-
210
-
211
- func updateProfileImage() {
212
-
213
- guard let image = selectedImage else { return }
214
-
215
-
216
-
217
- // UserService.shared.updateProfileImage(image: image) { (profileImageUrl) in
218
-
219
- // self.user.profileImageUrl = profileImageUrl
220
-
221
- // self.delegate?.controller(self, wantsToUpdate: self.user)
222
-
223
- // }
224
-
225
- }
226
-
227
-
228
-
229
- // MARK: - Helpers
230
-
231
-
232
-
233
- func configureNavigationBar(){
234
-
235
- navigationController?.navigationBar.barTintColor = .twitterBlue
236
-
237
- navigationController?.navigationBar.barStyle = .black
238
-
239
- navigationController?.navigationBar.isTranslucent = false
240
-
241
- navigationController?.navigationBar.tintColor = .white
242
-
243
- navigationItem.title = "Edit Profile"
244
-
245
- navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(handleCancel))
246
-
247
- navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handleDone))
248
-
249
- // navigationItem.rightBarButtonItem?.isEnabled = false
250
-
251
- }
252
-
253
-
254
-
255
- func configureTableView(){
256
-
257
- tableView.tableHeaderView = headerView
258
-
259
- headerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 180)
260
-
261
- headerView.delegate = self
262
-
263
-
264
-
265
- footerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 100)
266
-
267
- tableView.tableFooterView = footerView
268
-
269
- footerView.delegate = self
270
-
271
-
272
-
273
- tableView.register(EditProfileCell.self, forCellReuseIdentifier: reuseIdentifier)
274
-
275
- }
276
-
277
-
278
-
279
- func configureImagePicker(){
280
-
281
- imagePicker.delegate = self
282
-
283
- imagePicker.allowsEditing = true
284
-
285
- }
286
-
287
-
288
-
289
- }
290
-
291
-
292
-
293
- // MARK - UITableViewDataSource
294
-
295
-
296
-
297
- extension EditProfileController {
298
-
299
-
300
-
301
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
302
-
303
- return EditProfileOptions.allCases.count
304
-
305
- }
306
-
307
-
308
-
309
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
310
-
311
- let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! EditProfileCell
312
-
313
- cell.delegate = self
314
-
315
- guard let option = EditProfileOptions(rawValue: indexPath.row) else { return cell}
316
-
317
- cell.viewModel = EditProfileViewModel(user: user, option: option)
318
-
319
-
320
-
321
- return cell
322
-
323
- }
324
-
325
- }
326
-
327
-
328
-
329
- // MARK - UITableViewDelegate
330
-
331
-
332
-
333
- extension EditProfileController {
334
-
335
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
336
-
337
- guard let option = EditProfileOptions(rawValue: indexPath.row) else { return 0 }
338
-
339
- return option == .bio ? 100 : 48
340
-
341
- }
342
-
343
- }
344
-
345
-
346
-
347
- // MARK - EditProfileHeaderDelegate
348
-
349
-
350
-
351
- extension EditProfileController: EditProfileHeaderDelegate {
352
-
353
- func didTapChangeProfilePhoto(){
354
-
355
- present(imagePicker, animated: true, completion: nil)
356
-
357
- }
358
-
359
- }
360
-
361
-
362
-
363
- // MARK - UIImagePickerControllerDelegate
364
-
365
-
366
-
367
- extension EditProfileController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
368
-
369
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
370
-
371
- guard let image = info[.editedImage] as? UIImage else { return }
372
-
373
- self.selectedImage = image
374
-
375
- dismiss(animated: true, completion: nil)
376
-
377
- }
378
-
379
- }
380
-
381
-
382
-
383
- // MARK - EditProfileCellDelegate
384
-
385
-
386
-
387
- extension EditProfileController: EditProfileCellDelegate {
388
-
389
-
390
-
391
- func updateUserInfo(_ cell: EditProfileCell) {
392
-
393
- guard let viewModel = cell.viewModel else { return }
394
-
395
- userInfoChange = true
396
-
397
- navigationItem.rightBarButtonItem?.isEnabled = true
398
-
399
-
400
-
401
- switch viewModel.option {
402
-
403
- case .fullname:
404
-
405
- print("DEBUG: Update user fullname")
406
-
407
- guard let fullname = cell.infoTextField.text else { return }
408
-
409
- // user.fullname = fullname
410
-
411
- case .username:
412
-
413
- print("DEBUG: Update user Username")
414
-
415
- guard let username = cell.infoTextField.text else { return }
416
-
417
- // user.username = username
418
-
419
- case .bio:
420
-
421
- print("DEBUG: Update user bio")
422
-
423
- // user.profileText = cell.bioTextView.text
424
-
425
- }
426
-
427
-
428
-
429
- }
430
-
431
-
432
-
433
- }
434
-
435
-
436
-
437
- // MARK - EditProfileFooterDelegate
438
-
439
-
440
-
441
- extension EditProfileController: EditProfileFooterDelegate {
442
-
443
-
444
-
445
- func handleLogout() {
446
-
447
- let alert = UIAlertController(title: nil, message: "Are you sure you want to log out?", preferredStyle: .actionSheet)
448
-
449
- alert.addAction(UIAlertAction(title: "Log Out", style: .destructive, handler: { _ in
450
-
451
- self.dismiss(animated: true) {
452
-
453
- self.delegate?.handleLogout()
454
-
455
- }
456
-
457
- }))
458
-
459
-
460
-
461
- alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
462
-
463
- present(alert, animated: true, completion: nil)
464
-
465
- }
466
-
467
-
468
-
469
- }
470
-
471
-
472
-
473
- ```
474
-
475
-
476
-
477
- 試しに、下のコードのように、AppdelegateのdidFinishLaunchingWithOptionsで値を見てみたのですが、何も出なかったです。
478
-
479
- ```ここに言語を入力
480
-
481
- let realm = try! Realm()
482
-
483
- let initUserData: Results<User> = realm.objects(User.self).filter("id == 0")
484
-
485
- print("initUserData: (initUserData)")
486
-
487
- ```
488
-
489
- 調べても、他に情報がなかったので、どなたかご教授お願いします。