Ios NSPredicate与可转换属性一起使用

Ios NSPredicate与可转换属性一起使用,ios,swift,core-data,nspredicate,nsfetchrequest,Ios,Swift,Core Data,Nspredicate,Nsfetchrequest,我有一个字符串数组,作为可转换的字符串保存在核心数据中。从json中,我获得了如下所需的字符串: if let mealTimes = dictionary["mealTimes"] as? [String]{ self.mealTimes = mealTimes } NSPredicate(format: "mealtimes contains[cd] %@", "breakfast") 现在,我想在mealTime属性中按字符串过滤获取结果。我试过这样做: let predic

我有一个字符串数组,作为可转换的字符串保存在核心数据中。从json中,我获得了如下所需的字符串:

if let mealTimes = dictionary["mealTimes"] as? [String]{
     self.mealTimes = mealTimes
}
NSPredicate(format: "mealtimes contains[cd] %@", "breakfast")
现在,我想在mealTime属性中按字符串过滤获取结果。我试过这样做:

let predicate = NSPredicate(format: "mealTimes LIKE[cd] '*%@*'", "breakfast")
var breakfastContents = StaticContent.fetchStaticContentsWithPredicate(predicate, inManagedObjectContext: self.coreDataStack.context)
if (breakfastContents.count > 0) {
    breakfast = breakfastContents.first!
}
问题是结果数组是空的,但我知道我在某些内容中有早餐字符串。那我该怎么修呢?我在读一些关于将transformable保存为NSData的内容,因此需要一些很棒的技巧。我试图使用
LIKE
(带*和不带*)命令和
CONTAINS

静态内容的扩展:

class func fetchStaticContentsWithPredicate(predicate: NSPredicate, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> [StaticContent] {

    // Define fetch request/predicate
    var fetchRequest = NSFetchRequest(entityName: "StaticContent")

    // Assign fetch request properties
    fetchRequest.predicate = predicate
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]


    // Handle results
    let fetchedResults = managedObjectContext.executeFetchRequest(fetchRequest, error: nil) as! [StaticContent]
    return fetchedResults
}

您应该重构您的数据模型以摆脱“字符串数组”。这不是一种很好的存储此类值的方法

相反,考虑与捕获这些字符串的新实体的关系。或者,设置一个方案,在该方案中,您存储一个字符串,并具有分隔记录的唯一字符(例如

或类似的字符)。然后任何简单的字符串谓词都可以工作,如下所示:

if let mealTimes = dictionary["mealTimes"] as? [String]{
     self.mealTimes = mealTimes
}
NSPredicate(format: "mealtimes contains[cd] %@", "breakfast")
不能使用谓词筛选可转换属性的内容,请进行比较。