针对多种条件过滤核心数据对象

时间:2015-09-04 08:08:11

标签: swift core-data

处理我的应用程序的一部分,涉及过滤发送到tableview的对象。我尝试过使用CompoundNSPredicate但结果没有显示出来。如果我单独执行每个过滤器,我可以为一种类型的对象调用fetchrequest,但是当我尝试使用第二行在同一循环条件下调用第二类对象时,第二个对象根本不会出现。我在哪里的例子:

let fetchRequest = NSFetchRequest(entityName: "Cards")
    let warriorpredicate = NSPredicate(format: "cardClass = %i", 1)
    let shamanpredicate = NSPredicate(format: "cardClass = %i", 2)
    let roguepredicate = NSPredicate(format: "cardClass = %i", 3)
    let paladinpredicate = NSPredicate(format: "cardClass = %i", 4)
    let hunterpredicate = NSPredicate(format: "cardClass = %i", 5)
    let druidpredicate = NSPredicate(format: "cardClass = %i", 6)
    let warlockpredicate = NSPredicate(format: "cardClass = %i", 7)
    let magepredicate = NSPredicate(format: "cardClass = %i", 8)
    let priestpredicate = NSPredicate(format: "cardClass = %i", 9)
    let minionpredicate = NSPredicate(format: "cardClass = %i", 10)
    let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]
    if ((passedDeckObject?.classSelected.classname = "Warrior" ) != nil) {
        fetchRequest.predicate = warriorpredicate
    } else {
        if ((passedDeckObject?.classSelected.classname = "Shaman" ) != nil) {
            fetchRequest.predicate = shamanpredicate
            fetchRequest.predicate = minionpredicate
        } else {
            if ((passedDeckObject?.classSelected.classname = "Rogue" ) != nil) {
                fetchRequest.predicate = roguepredicate
            }

            if ((passedDeckObject?.classSelected.classname = "Paladin" ) != nil) {
                fetchRequest.predicate?.isEqual(paladinpredicate)
                fetchRequest.predicate?.isEqual(minionpredicate)
            } else {

                if ((passedDeckObject?.classSelected.classname = "Hunter" ) != nil) {
                    fetchRequest.predicate?.isEqual(hunterpredicate)
                    fetchRequest.predicate?.isEqual(minionpredicate)
                } else {
                    if ((passedDeckObject?.classSelected.classname = "Druid" ) != nil) {
                        fetchRequest.predicate?.isEqual(druidpredicate)
                        fetchRequest.predicate?.isEqual(minionpredicate)
                    } else {
                        if ((passedDeckObject?.classSelected.classname = "Warlock" ) != nil) {
                            fetchRequest.predicate?.isEqual(warlockpredicate)
                            fetchRequest.predicate?.isEqual(minionpredicate)
                        } else {
                            if ((passedDeckObject?.classSelected.classname = "Mage" ) != nil) {
                                fetchRequest.predicate?.isEqual(magepredicate)
                                fetchRequest.predicate?.isEqual(minionpredicate)
                            } else {
                                if ((passedDeckObject?.classSelected.classname = "Priest" ) != nil) {
                                    fetchRequest.predicate = priestpredicate
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return fetchRequest

}

有些代码看起来有所不同,但这是因为我用不同的方式测试每一个代码我使用我试过的两种方式减去了复合预测,但没有显示任何内容。

1 个答案:

答案 0 :(得分:0)

对此的回答有多种方式。有许多方法可以用来实际做我想做的事情,但最简单最优雅的解决方案是:

func listFetchRequest() -> NSFetchRequest {

    let requiredClassName = passedDeckObject!.classSelected.classname
    var requiredCardClasses = [Int]()
    let fetchRequest = NSFetchRequest(entityName: "Cards")
    let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
    switch(requiredClassName) {
        case "Warrior":
        requiredCardClasses = [1,10]
        case "Shaman":
        requiredCardClasses = [2,10]
        case "Rogue":
        requiredCardClasses = [3,10]
        case "Paladin":
        requiredCardClasses = [4,10]
        case "Hunter":
        requiredCardClasses = [5,10]
        case "Druid":
        requiredCardClasses = [6,10]
        case "Warlock":
        requiredCardClasses = [7,10]
        case "Mage":
        requiredCardClasses = [8,10]
        case "Priest":
        requiredCardClasses = [9,10]
        default:
        requiredCardClasses = [10]
    }
    fetchRequest.sortDescriptors = [sortDescriptor]
    fetchRequest.predicate = NSPredicate(format:"cardClass IN %@", requiredCardClasses)

    return fetchRequest

}