如何使用“IN”具有谓词的运算符 [英] How to use the "IN" Operator with a Predicate

查看:155
本文介绍了如何使用“IN”具有谓词的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写NSPredicate来获取与数组中的值匹配的行?例如:

How to write NSPredicate to fetch the rows that matches with values in the array? E.g:

[NSPredicate predicateWithFormat:@"UserName IN %@",UserIDArray];

如果 UserIDArray 包含用户ID,如何是否会获取与此数组中的值匹配的用户名?

If UserIDArray contains ID of users, how does one fetch the user name which matches with the values in this array?

推荐答案

让我先从这个建议开始。核心数据不是SQL。实体不是表格。对象不是行。列不是属性/属性。核心数据是一个对象图管理系统,可能会或可能不会持久保存对象图,并且可能会或可能不会使用远远落后的SQL来执行此操作。试图用SQL术语来思考核心数据将导致你完全误解核心数据并导致更多的悲伤和浪费时间。

Let me start with this advice. Core Data is not SQL. Entities are not tables. Objects are not rows. Columns are not attributes/properties. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.

因为核心数据是基于对象的,所以你做一个获取,你得到一个对象数组。要查找特定值,您可以在这些对象中查询其各自属性的值。

Because Core Data is based on objects, when you do a fetch, you get back an array of objects. To find a particular values, you then query those objects for the values of their respective attributes.

假设您有以下实体:

User{
    userName:string
    userID:string
}

...并且想要找到其userName对应于userID字符串数组的所有用户。您将创建NSFetchRequest并将其实体设置为User。然后你将使用这样的谓词:

... and want to find all the user's whose userName correspond to an array of userID strings. You would create a NSFetchRequest and set it's entity to "User". Then you would use a predicate like this:

[NSPredicate predicateWithFormat:@"userID IN %@",arrayOfUserIDs];

fetch将返回一个NSManagedObjects(或子类)数组,配置了属性名称键用户实体。要查找数组中每个托管对象的 userName 属性的值,您可以向对象询问其属性的值,如下所示:

The fetch will return an array of NSManagedObjects (or a subclass) configured with the attribute name keys of the User entity. To find the value of the userName attribute of each managed object in the array you would ask the object for the value of it's attribute like so:

NSString *aUserName=[aUserEntityObject valueForKey:@"userName"];

忘掉你所知道的关于SQL或类似数据库系统的一切。核心数据并不是真正的数据库,所以试图用这些术语来思考它只会让你感到困惑。核心数据都是关于对象的。

Just forget everything you know about SQL or similar database systems. Core Data is not truly a database so trying to think of it in those terms will just confuse you. Core Data is all about objects.

这篇关于如何使用“IN”具有谓词的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆