###前提・実現したいこと
userdefaultsで永続化している部分をrealmで管理したいです。
メモ帳アプリです。
###該当のソースコード
swift
1// 2// MemoTableTableViewController.swift 3// MyMemoApp 4// 5// Created by ———on 2017/12/27. 6// Copyright © 2017年 Dotinstall. All rights reserved. 7// 8 9import UIKit 10 11class MemoTableTableViewController: UITableViewController { 12 13 //データが保存されるようにする 14 //UserDefaults 15 let userDefaults = UserDefaults.standard 16// var memos = ["blue", "red", "pink"] 17 var memos = [String]() 18 19 //saveがexitに行く時に実行される 20 @IBAction func unwindToMemoList(sendor: UIStoryboardSegue) { 21 guard let sourceVC = sendor.source as? MemoViewController, let memo = sourceVC.memo else { 22 return 23 } 24 //データの上書き 25 if let selectedIndexPath = self.tableView.indexPathForSelectedRow { 26 self.memos[selectedIndexPath.row] = memo 27 } else { 28 self.memos.append(memo) 29 } 30 self.userDefaults.set(self.memos, forKey: "memos") 31 self.tableView.reloadData() 32 } 33 34 override func viewDidLoad() { 35 super.viewDidLoad() 36 37 if self.userDefaults.object(forKey: "memos") != nil { 38 self.memos = self.userDefaults.stringArray(forKey: "memos")! 39 } else { 40 self.memos = ["memo1", "memo2", "memo3"] 41 } 42 43 // Uncomment the following line to preserve selection between presentations 44 // self.clearsSelectionOnViewWillAppear = false 45 46 // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 47 // self.navigationItem.rightBarButtonItem = self.editButtonItem 48 } 49 50 override func didReceiveMemoryWarning() { 51 super.didReceiveMemoryWarning() 52 // Dispose of any resources that can be recreated. 53 } 54 55 // MARK: - Table view data source 56 57 //リストが幾つのセクションに分かれているのかを返す 58 override func numberOfSections(in tableView: UITableView) -> Int { 59 // #warning Incomplete implementation, return the number of sections 60 return 1 61 } 62 //何行あるのか返す 63 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 64 // #warning Incomplete implementation, return the number of rows 65 return self.memos.count 66 } 67 68 //実際にデータを表示するメソッドリストの行であるセルを作ってそれを返す。 69 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 70 let cell = tableView.dequeueReusableCell(withIdentifier: "MemoTableViewCell", for: indexPath) 71 72 // Configure the cell... 73 cell.textLabel?.text = self.memos[indexPath.row] 74 cell.detailTextLabel?.text = self.memos[indexPath.row] 75 76 return cell 77 } 78 79 //ヘッダーをつけるメソッド 80// override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 81// return "section-(section)" 82// } 83 84 85 /* 86 // Override to support conditional editing of the table view. 87 override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 88 // Return false if you do not want the specified item to be editable. 89 return true 90 } 91 */ 92 93 //スワイプした行を削除 94 // Override to support editing the table view. 95 override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 96 if editingStyle == .delete { 97 // Delete the row from the data source 98 self.memos.remove(at: indexPath.row) 99 self.userDefaults.set(self.memos, forKey: "memos") 100 tableView.deleteRows(at: [indexPath], with: .fade) 101 } 102 } 103 104 105 /* 106 // Override to support rearranging the table view. 107 override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { 108 109 } 110 */ 111 112 /* 113 // Override to support conditional rearranging of the table view. 114 override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 115 // Return false if you do not want the item to be re-orderable. 116 return true 117 } 118 */ 119 120 // 121 // MARK: - Navigation 122 123 // In a storyboard-based application, you will often want to do a little preparation before navigation 124 //セグエで遷移の前に実行される 125 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 126 // Get the new view controller using segue.destinationViewController. 127 // Pass the selected object to the new view controller. 128 guard let identifier = segue.identifier else { 129 return 130 } 131 if identifier == "editMemo" { 132 let memoVC = segue.destination as! MemoViewController 133 memoVC.memo = self.memos[(self.tableView.indexPathForSelectedRow?.row)!] 134 } 135 } 136 137 138} 139
###試したこと
realmの環境は整っているのですが、配列をどのようにrealmで管理するか分かりません。ググってもインスタンス化してからの処理しか見つけられませんでした。どなたか回答よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/01/04 15:31