Iphone 将NSPredicate与嵌套数组一起使用
我有一个这样的数组Iphone 将NSPredicate与嵌套数组一起使用,iphone,ios,arrays,nested,nspredicate,Iphone,Ios,Arrays,Nested,Nspredicate,我有一个这样的数组 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] in
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
只是()
任何有帮助的想法都会很好。谢谢。这里是阵列的问题
(
(
{
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";
}
)
)
编辑:-像这样尝试
(
{
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";
}
)
)
NSArray*questionData=[[NSArray alloc]initWithObjects:level1、level2、level3、nil];
NSPredicate*resultPredicate=[NSPredicate predicateWithFormat:@“名称包含‘the’”);
NSMutableArray*resultarray=[[NSMutableArray alloc]init];
对于(int i=0;i0)
[resultarray addObject:searchResults];
}
NSLog(@“%@”,结果数组);
O/P:-
(
(
{
name=“up的反面是什么?”;
questionpicture=“101q.png”;
},
{
name=“前面的反面是什么?”;
questionpicture=“102q.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";
}
)
)
为了处理上述谓词,数组必须如下所示
(
{
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";
}
)
)
编辑:-像这样尝试
(
{
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";
}
)
)
NSArray*questionData=[[NSArray alloc]initWithObjects:level1、level2、level3、nil];
NSPredicate*resultPredicate=[NSPredicate predicateWithFormat:@“名称包含‘the’”);
NSMutableArray*resultarray=[[NSMutableArray alloc]init];
对于(int i=0;i0)
[resultarray addObject:searchResults];
}
NSLog(@“%@”,结果数组);
O/P:-
(
(
{
name=“up的反面是什么?”;
questionpicture=“101q.png”;
},
{
name=“前面的反面是什么?”;
questionpicture=“102q.png”;
}
)
)
我使用子查询来解决这个问题
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";
}
)
)我使用子查询来解决这个问题
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";
}
)
)太好了。谢谢。这个问题可以用子查询来解决,不需要使用for循环或更改数组结构。太好了。谢谢。这个问题可以通过子查询解决,不需要使用for循环或更改数组结构。