简体   繁体   English

我如何在解析iOS中获得AND子句

[英]How can i get AND clause in parse ios

i am using this link as a guide, but i can figure out how can i use AND clause for parse. 我正在使用此链接作为指南,但是我可以弄清楚如何使用AND子句进行解析。 It only defines OR clause in sample code. 它仅在示例代码中定义OR子句。

if i were to convert it from SQL statement, it looks like this... 如果我要从SQL语句转换它,它看起来像这样...

SELECT * FROM RemittanceTable WHERE beneCode = 'code' AND remittanceAmount = 500.00

For example in this code, i want to check if the code AND amount if correct before reading the data. 例如在这段代码中,我想在读取数据之前检查代码和金额是否正确。 But it validates with OR clause 但是它用OR子句验证

PFQuery *beneCodeQuery = [PFQuery queryWithClassName:@"RemittanceTable"];
[beneCodeQuery whereKey:@"beneCode" containsString:_beneCodeText.text];

PFQuery *amountQuery = [PFQuery queryWithClassName:@"RemittanceTable"];
[amountQuery whereKey:@"remittanceAmount" equalTo:amount];

PFQuery *query = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:beneCodeQuery,amountQuery,nil]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        for (PFObject *object in objects) {
            NSLog(@"%@", object);

            [self resignAllResponder];
            _paymentDetailsImage.frame = CGRectMake(_paymentDetailsImage.frame.origin.x, _paymentDetailsImage.frame.origin.y, _paymentDetailsImage.frame.size.width, paymentModeOrigHeight);
            _detailsView.alpha = 1;

        }
    }}}];

Each time you add a constraint, it is implicitly using AND: 每次添加约束时,都会隐式使用AND:

[query whereKey:@"username" equalTo:@"admin"];
[query whereKey:@"location" equalTo:@"USA"];

translates to "username == 'admin' AND location == 'USA'" 转换为“用户名=='管理员'AND位置=='美国'”

UPDATE UPDATE

Your specific case as an example: 以您的具体情况为例:

PFQuery *beneCodeQuery = [PFQuery queryWithClassName:@"RemittanceTable"];
[beneCodeQuery whereKey:@"beneCode" containsString:_beneCodeText.text];
[beneCodeQuery whereKey:@"remittanceAmount" equalTo:amount];

NSArray *result = [beneCodeQuery findObjects];

I came across this answer by Héctor Ramos on Parse.com forum recently: 我最近在Parse.com论坛上遇到了HéctorRamos的回答:

You can indeed have multiple constraints on a single query, which will be treated as an AND. 实际上,您在单个查询上可以有多个约束,这些约束将被视为AND。 Having the same constraint more than once for the same key, is not supported. 不支持对同一键多次具有相同的约束。 In this case you have two whereKey:equalTo: constraints over "members", so only the last one is being considered. 在这种情况下,您对“成员”有两个whereKey:equalTo:约束,因此仅考虑最后一个。

Instead you'll want to use whereKey:containsAllObjectsInArray:. 相反,您将要使用whereKey:containsAllObjectsInArray:。

PFQuery *query = [PFQuery queryWithClassName:@"Conversation"];
[query whereKey:@"members" containsAllObjectsInArray:@[aFriend.data, [PFUser currentUser]]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {...}];

I hope it helps. 希望对您有所帮助。

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

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