NSpredicate 结果

Posted

技术标签:

【中文标题】NSpredicate 结果【英文标题】:NSpredicate Results 【发布时间】:2014-03-04 17:55:47 【问题描述】:

我有 2 个字典组成的数组。第一个字典按 Services 分组,并在包含我的第二个字典的数组中提供了具有相同服务的公司的数量以及所有详细信息。

第一个字典有 2 个键Companies and Services

在公司中,我有一个数组,其中包含我的第二个字典,其中包含以下键值

地址信息、名称和服务

我希望 NSpredicate 消除所有公司名称 我的第二个字典中与 searchText 不对应的对象。现在它的过滤过程只发生在第一个字典上。它不断给我第二本字典的所有结果,但只删除第一本字典(公司和服务)的对象。

提前致谢!

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K.%K CONTAINS[cd] %@",
                              @"Companies",@"Name",searchText];

    FilteredObject = [NSMutableArray arrayWithArray:[AllDataGroupbyServiceArray

filteredArrayUsingPredicate:predicate]];

(
    
    Companies =         (
                    
            AddressInformation = h3k2w3;
            Name = Teg;
            Service = "";
        
    );
    Services = "";
,
    
    Companies =         (
                    
            AddressInformation = j9g2k6;
            Name = Megan;
            Service = Electrician;
        ,
                    
            AddressInformation = h2k2w3;
            Name = test;
            Service = Electrician;
        ,
                    
            AddressInformation = h2t3b3;
            Name = Janet;
            Service = Electrician;
        ,
                    
            AddressInformation = "J4J 1H7";
            Name = Rick;
            Service = Electrician;
        ,
                    
            AddressInformation = "H1T 4B6";
            Name = Herber;
            Service = Electrician;
        ,
                    
            AddressInformation = "J0K 3B0";
            Name = test;
            Service = Electrician;
        ,
                    
            AddressInformation = h2k2w3;
            Name = test;
            Service = Electrician;
        ,
                    
            AddressInformation = h2k2w3;
            Name = canada;
            Service = Electrician;
        
    );
    Services = Electrician;
,
    
    Companies =         (
                    
            AddressInformation = J0L2K0;
            Name = "Colas Bn";
            Service = Mason;
        ,
                    
            AddressInformation = J5A1M2;
            Name = mrnoskill2;
            Service = Mason;
        ,
                    
            AddressInformation = J0L2K0;
            Name = test8;
            Service = Mason;
        ,
                    
            AddressInformation = J5C1S3;
            Name = test6;
            Service = Mason;
        ,
                    
            AddressInformation = h3r1z2;
            Name = Jason;
            Service = Mason;
        ,
                    
            AddressInformation = j7h2k7;
            Name = max;
            Service = Mason;
        ,
                    
            AddressInformation = J5Z2W5;
            Name = Fred;
            Service = Mason;
        ,
                    
            AddressInformation = "H4G 1K6";
            Name = Joe;
            Service = Mason;
        ,
                    
            AddressInformation = j3y7a6;
            Name = Tets;
            Service = Mason;
        
    );
    Services = Mason;
,
    
    Companies =         (
                    
            AddressInformation = h2k2w3;
            Name = jake;
            Service = Plumber;
        
    );
    Services = Plumber;

)

【问题讨论】:

【参考方案1】:

不要使用predicateWithFormat:,而是尝试predicateWithBlock:。这使您可以完全控制用于测试每个条目的过程。

我还建议您的模型很奇怪。您的数据模型可能需要更合适的结构。

【讨论】:

以上是关于NSpredicate 结果的主要内容,如果未能解决你的问题,请参考以下文章

使用 NSPredicate 的 FetchRequest 找不到结果?

Swift NSPredicate 返回所有结果而不是单个结果

NSPredicate,使用一对多关系的子集获取结果

过滤掉子类结果的 NSPredicate

NSPredicate 未按预期给出结果

Core Data NSPredicate 过滤结果