gpt4 book ai didi

swift - FetchedResultsController 使用谓词排除集合中包含的结果

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

我正在尝试过滤 FetchedResultsController 中的项目以过滤掉特定的一组项目。这些发动机具有特定的字母名称,后跟数字。名称中可能的字母范围是从“A”到“O”。我希望能够根据另一个条件过滤掉名称从“D”到“O”的引擎。

我设置了一个常量“highPowerEngines”,它是包含这些字母的集合。由于该名称仅包含一个字母,因此我想排除包含 highPowerEngines 中任何字母的任何名称。到目前为止,这是我的代码,我正在处理的谓词包含在 FIXME 之后:

func configureFetchedResultsController() {
let context = databaseController.getContext()
let enginesFetchRequest = NSFetchRequest<Engine>(entityName: CoreData.engine)
let highPowerEngines: Set = ["D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"]

var predicate = NSPredicate()
if currentStage == 1 && stages! == 1 {
predicate = NSPredicate(format: "engineType == %@", EngineType.singleStage.rawValue)
} else if currentStage < stages! {
predicate = NSPredicate(format: "engineType == %@", EngineType.boosterStage.rawValue)
} else {
predicate = NSPredicate(format: "engineType == %@", EngineType.upperStage.rawValue)
}

var predicateArray:[NSPredicate] = [
predicate
]
// FIXME: Sort out highPowerEngines
if !dPlusEngineIAP {
if currentStage == 1 && stages! == 1 {
predicate = NSPredicate(format: "NOT engineDesignation CONTAINS %@", highPowerEngines)
predicateArray.append(predicate)
}
}

let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicateArray)
enginesFetchRequest.predicate = compoundPredicate

let primarySortDescriptor = NSSortDescriptor(key: CoreData.isMadeByName, ascending: true)
let secondarySortDescriptor = NSSortDescriptor(key: CoreData.engineDesignation, ascending: true)
enginesFetchRequest.sortDescriptors = [primarySortDescriptor, secondarySortDescriptor]


self.fetchedResultsController = NSFetchedResultsController<Engine>(
fetchRequest: enginesFetchRequest,
managedObjectContext: context,
sectionNameKeyPath: CoreData.isMadeByName,
cacheName: nil)

self.fetchedResultsController.delegate = self

}

最佳答案

使用 IN 而不是 CONTAINS

NOT (engineDesignation IN %@)

更新

既然您想排除除树字母之外的所有内容,为什么不将谓词翻转为包含而不是排除,例如

engineDesignation BEGINSWITH ‘A’ OR
engineDesignation BEGINSWITH ‘B’ OR
engineDesignation BEGINSWITH ‘C’

关于swift - FetchedResultsController 使用谓词排除集合中包含的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51459478/

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