0

我有一系列这样的字典:

clients (
        {
        country = canada;
        city = vancouver;
    },
{
        country = Mexico;
        city = Mexico;
    },
{
        country = UK;
        city = London;
    },
{
        country = USA;
        city = NewYork;
    },
}

如果我使用 NSPredicate 在美国找到这样的客户:

NSPredicate *usaPredicte = [NSPredicate predicateWithFormat:@"SELF contains %@", @"USA"];
self.usa = [listOfClients filteredArrayUsingPredicate:usaPredicte];

工作得很好,但如果由于某种原因预测的结果为空,例如在这种情况下:

NSPredicate *peruPredicte = [NSPredicate predicateWithFormat:@"SELF contains %@", @"Peru"];
self.peru = [listOfClients filteredArrayUsingPredicate:peruPredicte];

我收到此错误:

-[__NSCFDictionary filteredArrayUsingPredicate:]: unrecognized selector sent to instance 0xdcb6e0

你们中的任何人都知道当谓词为空或为空时为什么或如何避免出现此错误?

我会非常感谢你的帮助

4

1 回答 1

0

try this

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(country ==  %@)", @"USA"];

NSArray *filteredArray = [listOfClients filteredArrayUsingPredicate:predicate];

Assuming filterArray always have count 1.

if (filteredArray.count ) 
     self.usa = [[filteredArray objectAtIndex: 0] objectForKey: @"country"];
于 2013-10-16T19:48:26.143 回答