简体   繁体   English

如何在Swift中返回Realm列表?

[英]How to return Realm list in Swift?

I have 2 Realm List 我有2个领域清单

and I'm trying to make healper function so I can choose between them to display one of them on UITableview 并且我正在尝试使功能恢复正常,因此我可以在它们之间进行选择以在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 当我尝试使用功能ChooseList时出现错误

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 我不知道该怎么做,是否还有另一种方法来存档我要存档的内容

In this case,arrayList1 is SomeOtherType. 在这种情况下,arrayList1是SomeOtherType。 List is not that type, with the as! 列表不是那个类型,用as! cast is dangerous.Always Use as? 强制转换很危险。始终用作?

Try this one, 试试这个

public var arrayList1 : DataType = List<realmList1>()
public var arrayList2 : DataType = 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 nil
}

i dont think that you need generics here if your realmList1 and realmList2 are inherited from Object you can write your chooseList method like this 我不认为如果您的realmList1和realmList2是从Object继承的,那么您就不需要泛型了,您可以像这样编写您的ChooseList方法

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

    if cehck == true {
        return arrayList1 as! List<realmList1> // or just return arrayList1
    } else if cehck == false {
        return arrayList2 as! List<realmList2> // or just return arrayList2

    }
    return arrayList1 as! List<realmList1>
}

I'll suggest an alternative solution. 我将建议一种替代解决方案。 This is macOS but very similar to iOS and it's really just to provide another option. 这是macOS,但与iOS非常相似,实际上只是提供了另一种选择。

Suppose we have a tableView and want to switch what's listed in the tableView depending on what the user selects. 假设我们有一个tableView,并希望根据用户选择的内容切换tableView中列出的内容。

Give two Realm objects RedGrape, WhiteGrape 给两个领域对象RedGrape,WhiteGrape

and then a class that contains lists of both of those called Grape. 然后是一个类,其中包含两个都称为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: 然后是一个在viewController中处理tableView的类,请注意,这只是概念上的:

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: 从这里您可以在Grape类中实现更多决策代码:

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 然后你的tableView numberOfRows变成

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM