Използване на NSPredicate с вложени масиви ios

Имам масив като този

NSMutableArray *level1;
NSMutableArray *level2;
NSMutableArray *level3;

self.questionSections=[[NSArray alloc] initWithObjects:@"Level 1",@"Level 2",@"Level 3", nil];

level1=[[NSMutableArray alloc] init];
level2=[[NSMutableArray alloc] init];
level3=[[NSMutableArray alloc] init];

[level1 addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
                   @"What is the opposite of up?",@"name",
                   @"101q.png",@"questionpicture",
                   nil]];

[level1 addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
                   @"What is the opposite of front?",@"name",
                   @"102q.png",@"questionpicture",
                   nil]];


[level2 addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
                   @"Name a common pet?",@"name",
                   @"201q.png",@"questionpicture",
                   nil]];


[level2 addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
                   @"Name a common bird?",@"name",
                   @"202q.png",@"questionpicture",
                   nil]];


[level3 addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
                   @"List 3 rooms in your house?",@"name",
                   @"301q.png",@"questionpicture",
                   nil]];

self.questionData=[[NSArray alloc] initWithObjects:level1,level2,level3, nil];

И искам да го потърся. Опитвам това, но не стигам до никъде

NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"name CONTAINS 'the'"];

NSLog(@"%@",resultPredicate);
NSLog(@"%@",self.questionData);

self.searchResults=[self.questionData  filteredArrayUsingPredicate:resultPredicate];

NSLog(@"%@",self.searchResults);

resultPredicate изглежда добре "името СЪДЪРЖА 'the'"

self.questionData изглежда добре

Но self.searchResults е просто ( )

Всякакви идеи за помощ биха били чудесни. Благодаря.


person OzRoz    schedule 18.07.2013    source източник
comment
stackoverflow.com/questions/1434291/   -  person user523234    schedule 18.07.2013


Отговори (2)


тук проблем с масива.

(
        (
                {
            name = "What is the opposite of up?";
            questionpicture = "101q.png";
        },
                {
            name = "What is the opposite of front?";
            questionpicture = "102q.png";
        }
    ),
        (
                {
            name = "Name a common pet?";
            questionpicture = "201q.png";
        },
                {
            name = "Name a common bird?";
            questionpicture = "202q.png";
        }
    ),
        (
                {
            name = "List 3 rooms in your house?";
            questionpicture = "301q.png";
        }
    )
)

за да работи вашият горен предикат, тогава масивът трябва да бъде така,

(
        {
        name = "What is the opposite of up?";
        questionpicture = "101q.png";
    },
        {
        name = "What is the opposite of front?";
        questionpicture = "102q.png";
    },
        {
        name = "Name a common pet?";
        questionpicture = "201q.png";
    },
        {
        name = "Name a common bird?";
        questionpicture = "202q.png";
    },
        {
        name = "List 3 rooms in your house?";
        questionpicture = "301q.png";
    }
)

РЕДАКТИРАНЕ: - опитайте така,

 NSArray *questionData=[[NSArray alloc] initWithObjects:level1,level2,level3, nil];

NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"name CONTAINS 'the'"];


NSMutableArray *resultarray= [[NSMutableArray alloc]init];
for(int i=0;i<[questionData count];i++){
    NSArray* searchResults=[[questionData objectAtIndex:i]  filteredArrayUsingPredicate:resultPredicate];
    if([searchResults count]>0)
        [resultarray addObject:searchResults];
}
NSLog(@"%@",resultarray);
O/P:-
 (
        (
                {
            name = "What is the opposite of up?";
            questionpicture = "101q.png";
        },
                {
            name = "What is the opposite of front?";
            questionpicture = "102q.png";
        }
    )
)
person Balu    schedule 18.07.2013
comment
Това е страхотно. Благодаря. - person OzRoz; 19.07.2013
comment
Проблемът може да бъде решен с помощта на подзаявка, няма нужда да използвате for цикъл или да променяте структурата на масива си. - person Puneet Sharma; 19.07.2013

Използвах Subquery, за да реша това

 NSArray *questionData=[[NSArray alloc] initWithObjects:level1,level2,level3, nil];

 NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"SUBQUERY(name, $content, $content CONTAINS %@).@count > 0", @"the"];

 NSArray *searchResults=[questionData  filteredArrayUsingPredicate:resultPredicate];

 NSLog(@"%@",searchResults);

Резултат ::

(
    (
            {
        name = "What is the opposite of up?";
        questionpicture = "101q.png";
    },
            {
        name = "What is the opposite of front?";
        questionpicture = "102q.png";
    }
)

)

person Puneet Sharma    schedule 18.07.2013