gpt4 book ai didi

ios - Swift 2.2,RealmSwift - 数据检索不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 07:22:03 24 4
gpt4 key购买 nike

我正在为我的记事本应用程序使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com