gpt4 book ai didi

swift - 根据多个条件过滤核心数据对象

转载 作者:行者123 更新时间:2023-11-30 14:06:01 25 4
gpt4 key购买 nike

正在处理我的应用程序的一部分,其中涉及过滤发送到 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

}

有些代码看起来不同,但这是因为我用不同的行测试了每一个代码,我使用我尝试过的两种方法给出了示例,减去了根本没有显示任何内容的复合谓词尝试。

最佳答案

这个问题的答案有多种。有很多方法可以用来实际做我想做的事情,但最简单最优雅的解决方案是:

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

}

关于swift - 根据多个条件过滤核心数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com