- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在为我的记事本应用程序使用 Realm 来存储数组 noteTitles,这是一个自定义类 Note。该应用程序运行良好,但是当它应该保存时,它不会通过。当我重新启动应用程序时,笔记消失了。我将为所有文件提供代码。此外,我希望用户添加注释并且我需要 ObjectForPrimaryKey 每次都更新以便为每个注释创建一个新的 id。
注意类代码:
import Foundation
import RealmSwift
class Note: Object {
dynamic var title = ""
var content = ""
var id = 0
override class func primaryKey() -> String? {
print("id generated")
return "id"
}
}
ViewController(我写注释的地方)代码:
import UIKit
import RealmSwift
var note2 = Note()
var note: Note!
let realm = try! Realm()
class ViewController3: UIViewController, UITextViewDelegate {
var note: Note!
@IBOutlet var noteText: UITextView!
@IBOutlet var noteTitle: UITextField!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
noteTitle.text = note.title
noteText.text = note.content
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
note.title = noteTitle.text!
note.content = noteText.text
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textViewDidEndEditing(textView: UITextView) {
func noteEnded(note2: Note) {
note2.title = note.title
note2.content = note.content
note2.id = note.id
do {
try realm.write {
realm.add(noteTitles, update: true)
print("added")
}
} catch {
print("There was a problem")
}
}
print("editing ended")
}
override func viewDidLoad() {
super.viewDidLoad()
print(note2.id)
self.noteText.delegate = self
// Do any additional setup after loading the view.
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
noteTitle.resignFirstResponder()
noteText.resignFirstResponder()
}
}
TableViewController(笔记列表)代码:
import UIKit
import RealmSwift
var noteTitles:[Note] = []
class TableViewController: UITableViewController {
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return noteTitles.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = noteTitles[indexPath.row].title // error here
// Configure the cell...
return cell
}
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
sleep(2)
if realm.objectForPrimaryKey(Note.self, key: 0) != nil {
realm.objectForPrimaryKey(Note.self, key: 0)
}
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
noteTitles.removeAtIndex(indexPath.row)
tableView.reloadData()
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier! == "editNote" {
let noteDetailViewController = segue.destinationViewController as! ViewController3
let selectedIndexPath = tableView.indexPathForSelectedRow
noteDetailViewController.note = noteTitles[selectedIndexPath!.row]
} else if segue.identifier! == "addNote" {
let note = Note()
noteTitles.append(note)
let noteDetailViewController = segue.destinationViewController as! ViewController3
noteDetailViewController.note = note
}
}
}
提前致谢!
最佳答案
noteEnded
函数不应该在 textViewDidEndEditing
中,并且它没有被调用。试试这个
func noteEnded(note2: Note) {
do {
try realm.write {
realm.add(note2, update: true)
print("added")
}
} catch {
print("There was a problem")
}
}
func textViewDidEndEditing(textView: UITextView) {
note2.title = note.title
note2.content = note.content
note2.id = note.id
noteEnded(note2)
print("editing ended")
}
最好有“完成”按钮来保存笔记。易于处理输入和保存。
关于ios - Swift 2.2,RealmSwift - 数据检索不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38363861/
我正在使用 RealmSwift 为我正在构建的 iOS 应用程序创建个人识别码对象。我已经创建了一个构造函数和一些基本函数来检查 pin、输入新 pin 等。我可以使用在 RealmSwift 中创
我已经使用 Realmsvift 在我的应用程序中创建了数据库。这是控制台中的输出。请告诉我,如何在应用程序中从中读取数据?例如,我想显示键值对:Oslo - 2.89。感谢您的帮助。 class V
我正在尝试从我的领域数据库中查询 token 和user_id,然后传递给我的 private var token 和userId,但是我一直抛出此错误 void函数中非预期的非无效返回值 在retu
假设我有很多城市对象。所以,城市 ID 将是我的主键,因为它们不能重复。将会有相同的城市名称,但是,不同的 ID 和不同的 ShopAddress。这是我的 json 结果这是来 self 的后端 {
当我使用 RealmSwift 时,它第一次工作并存储数据。然后,我使用 RealmBrowser 打开它。使用RealmSwift用Realm Browser打开Realm文件后,项目就解散了,错误
我有一个 Report 类,例如: class Report: Object{ @objc dynamic var id : String? = nil @objc dynamic var
这是我第一次在 Swift 上使用 Realm,尽管我通读了整个 Realm 文档,但我似乎无法理解一些简单而基本的概念。 当您在 Realm 中创建一个 Object 类并创建该类的实例时,它会自动
我一直在尝试安装新版本的 realm 0.92.3。我已按照教程中的每一步操作,但仍然不断收到错误消息。 btplpinoudaynpddpsugjevklbvh/Build/Products/Deb
我有这个模型: import Foundation import RealmSwift class Event: Object { dynamic var id
我已经在版本 0.98.2 中安装了 RealmSwift 的动态框架。(CocoaPods 不工作。) 我收到以下错误: module-includes>:1:9: note: in file in
let selectedConsoles = ["Xbox", "Playstation 4"] let players = realm.objects(Person).filter("console
我有一个带有一些字符串大小写的枚举 public enum UserPermission: String, Decodable { case userManagement = "USER_MA
我在理解避免将数据重复写入 Realm 数据库方面遇到了一些问题。我有以下对象模型 Person.swift class Person: Object { let id = RealmOpti
我不知道如何根据用户选择组合谓词。这是我的数据概览。 Person.swift class Person: Object { let id = RealmOptional() dynami
我如何知道特定交易何时完成? 我想在交易完成后运行一段特定的代码。我该怎么做? 我在下面的 war 中执行写入 - do { try realm.write({ realm.add()
我是编程新手,我想知道如何使用 RealmSwift 将数据存储在列表中。 考虑以下模型: import RealmSwift class ScanResults: Object{ dynamic v
我正在为我的项目使用 RealmSwift。但是,我不确定如何解决以下问题: RMLException: Attempting to modify object outside of a write
我有一些从 Realm 中获取的 item: let items = realm.objects(ShoppingListItem.self) print("\(items.count) items"
我有两个类(class): 第一个: class GameObject: Object { @objc dynamic var gameOutcome: String? = nil @
使用 RealmSwift 执行此操作的正确方法是什么,它曾经是 RLMobject var stream:Results stream = Realm().objects(streams) 第一个作
我是一名优秀的程序员,十分优秀!