【问题标题】:Storing class objects locally in swift 3在swift 3中本地存储类对象
【发布时间】:2017-11-09 05:37:57
【问题描述】:

我在 swift 中存储“笔记”时遇到了问题。

这里是笔记类

class Note {

    var title = ""
    var content = ""
    var color = UIColor()

}

我得到这样的笔记

var notes: [Note] = []

我添加了一些注释

    let note1 = Note()
    note1.title = "Houses"
    note1.content = "A knowledgable and experienced staff ready to help you build the… "
    note1.color = #colorLiteral(red: 0.515632689, green: 0.2357951403, blue: 0.9598689675, alpha: 1)

    let note2 = Note()
    note2.title = "Note"
    note2.content = "A beautifully redesined note app"
    note2.color = #colorLiteral(red: 0.8978558183, green: 0.7694990635, blue: 0.2824732065, alpha: 1)

    let note3 = Note()
    note3.title = "Note"
    note3.content = "A beautifully redesined note app"
    note3.color = #colorLiteral(red: 0.2296327353, green: 0.8767140508, blue: 0.5295107365, alpha: 1)

    let note4 = Note()
    note4.title = "Note"
    note4.content = "A beautifully redesined note app"
    note4.color = #colorLiteral(red: 0.8787152171, green: 0.4267094135, blue: 0.2448620498, alpha: 1)

    notes.append(note1)
    notes.append(note2)
    notes.append(note3)
    notes.append(note4)

现在我需要保存它们并检索这些笔记,以便用户可以在那里制作自己的笔记,他们将保存在本地设备上

感谢您的帮助:)

【问题讨论】:

  • C o r e D a t a
  • 如果你是初学者,我现在只存储在一个 .plist 文件中(它最终会是 XML)。见:stackoverflow.com/questions/27197658/…
  • 哦,不,我不是初学者,我已经编码 3-4 年了,只是从来没有接触过存储数组,我只在 userdata 中存储了整数和类似的东西

标签: arrays swift object local-storage


【解决方案1】:

选择一个 db 或等效的 db 并学习如何持久化数据。

核心数据。很明显

领域。我的偏好。你已经完成了一半的工作:

import RealmSwift

class Note: Object {

    dynamic var title = ""
    dynamic var content = ""
    dynamic var color = "" // rgb value of "FF03A4", for instance
}

火力基地。新的和令人兴奋的。

这一切都取决于您的需求。第一步:弄清楚您的需求是什么,然后参考每个以找到其优势。最重要的是,您通过以可检索(和保存)的方式定义数据来完成繁重的工作。您需要掌握数据持久性才能对您的应用执行最有用的操作(为您的用户记住和自定义)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-21
    • 2021-07-18
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 2018-02-16
    相关资源
    最近更新 更多