質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

ArrayList

Java用のタグです。arrayListはListインターフェースを実装した、リサイズ可能な配列用クラスです。

Q&A

解決済

1回答

2372閲覧

userdefaultsで永続化している部分をrealmで管理したいです。

退会済みユーザー

退会済みユーザー

総合スコア0

Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

ArrayList

Java用のタグです。arrayListはListインターフェースを実装した、リサイズ可能な配列用クラスです。

0グッド

1クリップ

投稿2017/12/28 17:14

編集2018/01/02 04:27

###前提・実現したいこと
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で管理するか分かりません。ググってもインスタンス化してからの処理しか見つけられませんでした。どなたか回答よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

がっつりRealmを扱ったことないため、参考になるかはわからないんですが、
https://realm.io/jp/docs/swift/latest
こちらのドキュメントにListクラスが対応されているようにのってます。ただListで格納できるのはRealmが提供しているObjectクラスのサブクラス(Objectクラスを継承したクラス)のように記述されてましたので、そうしたクラスを別で作る必要があるみたいですね。

また、わざわざListで保存しなくても、そちらのメモを一つずつ保存するのもいいのかなと思いました。

投稿2017/12/30 09:14

komo_ta

総合スコア275

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/01/04 15:31

返信が遅くなってすいません!わかりました!挑戦して見ます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問