簡體   English   中英

結合使用NSPredicate和嵌套數組iOS

[英]Using NSPredicate with nested arrays 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只是()

任何有幫助的想法都會很棒。 謝謝。

我用子查詢來解決這個問題

 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";
    }
)

這是數組的問題。

(
        (
                {
            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";
        }
    )
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM