0

I've ran into a problem storing "notes" in swift.

Here is the note class

class Note {

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

}

And i get the notes like this

var notes: [Note] = []

Ive added a couple notes

    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)

And now i need to save them and retrieve these notes so users can make there own notes and they will save on there device locally

Thanks for helping :)

matt
  • 515,959
  • 87
  • 875
  • 1,141
Tye Howatt
  • 27
  • 1
  • 2
    C o r e D a t a – vadian Jun 07 '17 at 18:14
  • If you are a beginner, I'd just store in a .plist file for now (it will end up being XML). See: https://stackoverflow.com/questions/27197658/writing-swift-dictionary-to-file – Lou Franco Jun 07 '17 at 18:30
  • oh no I'm not a beginner Ive been coding for 3-4 years now just never got into storing arrays I've only stored integers and stuff like that in userdata – Tye Howatt Jun 07 '17 at 18:33

1 Answers1

0

Pick a db or db equivalent and learn how to persist data.

Core data. Obviously

Realm. My preference. You've already done half the work:

import RealmSwift

class Note: Object {

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

Firebase. New and exciting.

It all depends on your needs. Step one: figure out what your needs are then refer to each to find its strengths. The bottom line is you've done the heavy lifting by defining your data in a way that can be retrieved (and saved). You need to master data persistence to do most useful things with your apps (remembering and customizing for your user).

Mozahler
  • 4,958
  • 6
  • 36
  • 56