简体   繁体   English

如何使用NSPredicate过滤存储在CoreData中的数组?

[英]How to use NSPredicate to filter an array stored in CoreData?

I have a 'tags' array stored in CoreData (type 'Transformable') as an Array. 我有一个'tags'数组存储在CoreData(类型'Transformable')中作为数组。 I am making a request to CoreData to retrieve tags based on a searchText (string) input. 我正在向CoreData请求基于searchText(字符串)输入检索标签。

How can I use NSPredicate to filter the tags array based on the searchText? 如何使用NSPredicate根据searchText过滤标签数组?

let request = NSFetchRequest<NSFetchRequestResult>(entityName: Constants.Trip)

if(searchText != nil && searchText != "") {
    request.predicate = NSPredicate(format: "ANY %@ in tags", searchText!)
}

request.returnsObjectsAsFaults = false

You cannot do that. 你不能这样做。 Check this answer core data array of string filter 检查此答案核心数据库的字符串过滤器数组

You might consider to have an entity for tags, or join the array of tags and have it as a whole string in your current entity, thus having the tags property as type String. 您可以考虑为标记创建实体,或者加入标记数组并将其作为整个字符串放在当前实体中,从而将tags属性设置为String类型。 For the last approach, you could search with the predicate below. 对于最后一种方法,您可以使用下面的谓词进行搜索。

 NSPredicate(format: "tags CONTAINS[cd] %@", searchText)

The [cd] expression is to specify case and diacritic insensitivity respectively. [cd]表达式分别用于指定case和diacritic insensitivity。 You can get know more about the Core Data operations here . 您可以在此处了解有关核心数据操作的更多信息。

I got some basic operators from the documentation and put them below: 我从文档中获得了一些基本操作符并将它们放在下面:

BEGINSWITH The left-hand expression begins with the right-hand expression. BEGINSWITH左手表达式以右手表达式开头。

CONTAINS The left-hand expression contains the right-hand expression. CONTAINS左手表达式包含右手表达式。

ENDSWITH The left-hand expression ends with the right-hand expression. ENDSWITH左手表达式以右手表达式结束。

LIKE The left hand expression equals the right-hand expression: ? LIKE左手表达式等于右手表达式:? and * are allowed as wildcard characters, where ? 和*被允许作为通配符,在哪里? matches 1 character and * matches 0 or more characters. 匹配1个字符,*匹配0个或更多个字符。

You need to write predicate and set as a part of request like this ... 你需要编写谓词并设置为这样的请求的一部分......

func getEntitiesWithName(_ entityName:String, sortKey:String?, predicate: NSPredicate?, ascending:Bool, context:NSManagedObjectContext) -> [NSManagedObject] {

    var results:[NSManagedObject] = [NSManagedObject]()

    context.performAndWait { () -> Void in

        let request = NSFetchRequest<NSFetchRequestResult>()
        request.entity = NSEntityDescription.entity(forEntityName: entityName, in: context)

        if let fetchPredicate = predicate {
            request.predicate = fetchPredicate
        }

        if let key = sortKey {

            let sortDescriptor = NSSortDescriptor(key: key, ascending: ascending, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))
            request.sortDescriptors = [sortDescriptor];
        }

        results = (try! context.fetch(request)) as! [NSManagedObject]
    }

    return results
}

And you can set predicate like this 你可以像这样设置谓词

let predicate = NSPredicate(format: "isActive == %@", NSNumber(value: true as Bool))

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

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