2

I'm new to Realm and have been through the documentation a few times. I need to persist a [[String]] and have not found a way to do it yet

 var tableViewArray = [[String]]()  

I see the documentation pointing to Lists but I've been unsuccessful at implementing them. I'm showing my whole process here but just need help persisting my var tableViewArray = [[String]]()in Realm

This is my class

class TableViewArrays {

var tableViewArray = [[String]]()  // populates the Main Tableview

/// add picker selection to tableview array
func appendTableViewArray(title: String, detail: String, icon: String ) {

        var newRow = [String]()
        newRow.append(title)
        newRow.append(detail)
        newRow.append(icon)
        tableViewArray.append(newRow)
}

In the View Controller I instantiate the object

var tableViewArrays = TableViewArrays() 

Then call the class function to populate the object

var tableViewArrays.appendTableViewArray(title: String, detail: String, icon: String )

Thank you for taking a look

3
  • Can you post the code where the Realm object you're persisting is declared? Commented Feb 13, 2017 at 0:00
  • I believe I have a solution for this. Here are the objects: Commented Feb 13, 2017 at 0:08
  • ˚Sorry -- have trouble adding code blocks -- class TableViewRow: Object { dynamic var icon = "" dynamic var title = "" dynamic var detail = "" override var description: String { return "TableViewRow {(icon), (title), (detail)}" } } class EventTableView: Object { let rows = List<TableViewRow>() //override var description: String { return "Car {(brand), (name), (year)}" } } Commented Feb 13, 2017 at 0:10

2 Answers 2

2

I would make two Realm objects to be persisted, then nest them. Here's an example:

class RealmString: Object {
    dynamic var value = ""
}

class RealmStringArray: Object {
    let strings = List<RealmString>()
}

class TableViewArray{
    let stringArrays = List<RealmStringArray>()
}

I can't say much about the efficiency of this method, but I suppose it should work for your purpose. Also, if you have a large amount of data, it may become a pain to persist each individual string, then string collection, the string collection collection.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Faris, nice solution and I appreciate the help!
This is the recommended way to do it for now. Storing arrays of primitives is on the roadmap for Realm (github.com/realm/realm-cocoa/issues/1120), so hopefully this will be a better experience in future. :)
Feel free to mark a solution you're satisfied with as correct! @user6530825
1

create the classes

class TableViewRow: Object {
    dynamic var icon = ""
    dynamic var title = ""
    dynamic var detail =  ""

    override var description: String { 
    return "TableViewRow {\(icon), \(title), \(detail)}" }
}

class EventTableView: Object {
    let rows = List<TableViewRow>()
}

then instantiate the objects and append

let defaultTableview = EventTableView()
let rowOne = TableViewRow()

rowOne.icon = "man icon" ; rowOne.title = "War Hans D.O.P." ; rowOne.detail = "Camera Order Nike 2/11/17"

defaultTableview.rows.append(objectsIn: [rowOne])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.