Search code examples
swiftrealmrealm-mobile-platform

How to return Realm list in Swift?


I have 2 Realm List

and I'm trying to make healper function so I can choose between them to display one of them on UITableview

my model looks like this

public class RealmModel : Object {

    public var arrayList1 = List<realmList1>()
    public var arrayList2 = List<realmList2>()


    func ChooseList<T> (cehck : Bool) ->  List<T> {

        if cehck == true {
            return arrayList1 as! List<T>
        } else if cehck == false {
            return arrayList2 as! List<T>

        }
        return arrayList1 as! List<T>
    }


}

I'm trying using Generic Type but with no luck

when I try to use the function ChooseList I got error

Generic parameter 'T' could not be inferred

I don't know how to do it , and if there is another way to archive what i'm trying to archive


Solution

  • I'll suggest an alternative solution. This is macOS but very similar to iOS and it's really just to provide another option.

    Suppose we have a tableView and want to switch what's listed in the tableView depending on what the user selects.

    Give two Realm objects RedGrape, WhiteGrape

    and then a class that contains lists of both of those called Grape. This is similar to what's in the original question but I wanted to use a more well defined item for use in our lists.

    class RedGrape: Object {
        @objc dynamic var grapeName = ""
    }
    
    class WhiteGrape: Object {
        @objc dynamic var grapeName = ""
    }
    
    class Grape: Object {
        let reds = List<RedGrape>()
        let whites = List<WhiteGrape>()
    }
    

    and then a class that handles the tableView within a viewController, note this is just conceptual:

    class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
        @IBOutlet weak var myTableView: NSTableView!
    
        var self.userSelection = 0 //changed by user: 0 for red, 1 for white
        var myGrape = //set up the Grape Object var from Realm
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.userSelection = 0
    
            self.myTableView.delegate = self
            self.myTableView.dataSource = self
    
            self.myTableView.reloadData()
        }
    
    
        func numberOfRows(in tableView: NSTableView) -> Int {
            if self.userSelection == 0 {
                return self.myGrape.reds.count
            } else {
                return self.myGrape.white.count 
            }
        }
    
        func tableView(_ tableView: NSTableView, 
                       viewFor tableColumn: NSTableColumn?,
                       row: Int) -> NSView? {
            var name = ""
            if self.userSelection == 0 {
                name = self.myGrape.reds[row].grapeName
            } else {
                name = self.myGrape.whites[row].grapeName
            }
    
            let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue:"MyCellView")
    
            if let cellView = self.myTableView.makeView(withIdentifier: cellIdentifier, 
                                                     owner: nil) as? NSTableCellView {
                cellView.textField?.stringValue = name
                return cellView
            }
    
            return nil
        }
    }
    

    from here you could implement more decision making code within the Grape class:

    class Grape: Object {
        let reds = List<RedGrape>()
        let whites = List<WhiteGrape>()
    
        func rowCountFor(selection: Int) -> Int {
          if selection == 0 {
             return self.Grape.reds.count
          } else {
             return self.Grape.white.count 
          }
    
        func nameFor(selection: Int, forRow: Int) -> String {
           if selection == 0 {
             return self.reds[forRow]
          } else {
             return self.whites[forRow]
          }
        }
    }
    

    then your tableView numberOfRows becomes

        func numberOfRows(in tableView: NSTableView) -> Int {
            self.myGrape.rowCountFor(selection: self.userSelection)
        }