开发者

how to search nsmutable array in objective C

开发者 https://www.devze.com 2023-01-05 04:37 出处:网络
can any body tell me how to search NSMutable Arry in which objects are stored from xml feed i have the following code

can any body tell me how to search NSMutable Arry in which objects are stored from xml feed i have the following code

- (void) searchTableView {

NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
//blog entries is the nsmutable array in which objects are stored from RSS feed
for (NSMutableDictionary *dictionary in blogEntries)
{ 
    NSArray *images=[dictionary objectForKey:@"image"];
    NSArray *titlearray = [dictionary objectForKey:@"title"];
    NSDictionary *imagesDic=[NSDictionary dictionaryWithObject:images forKey:@"image"];
    NSDictionary *titlearrayDic=[NSDictionary dictionaryWithObject:titlearray forKey:@"title"];
    [searchArray addObject:imagesDic];
    [searchArray addObject:titlearrayDic];
}
//know the problem comes in below code i just want to compare title not image string as there any way to search only of title array not for both image in title some what like this For(nsstring *stemp in searcArray.titleArray etc)
for (NSString *sTemp in searchArray)
{
    NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

    if (titleResultsRange.length > 0){

            [copyListOfItems addObject:sTemp];

    }}

the problem is that this code just saving title not image and if i save image then it also search in image string which i do开发者_StackOverflownt want to do. i want the user will search only by title then when he type something in textbox if search is true against some values then only thos e are displayed in table cell with title and image. as this is RSS APPLiction and feeds are comming from xml feed which click here bescially i am extracting this xml feed and em displaying image and title tage in table cell know i want to implement searchbar in it Thanks....i am waiting for your response...


@mipadi is right - try using containsObject: first.

If that doesn't work, just a simple loop will do it - you can put in whatever matching criteria you want in there. e.g. This code searches by comparing the name properties :

- (id)searchArray:(NSArray *)haystack for:(id)needle {
    for(id temp in haystack)
        if ([temp name] isEqual:[needle name]])
            return needle;

    return nil;
}

Hope that helps.

NB If you're using your own objects in the array, you can use containsObject: if you have overridden isEqual: (and hash)


Depends on how you want to search. If you're just looking for a particular object, you can use containsObject:.


Without knowing more about what you want, it's tough to answer your question. But here are some starting points:

  1. NSArray - Check out the methods starting like indexOfObject-; I think one of these probably does what you want. There's also filteredArrayUsingPredicate.
  2. NSMutableArray - The only notable method here is filterUsingPredicate, I think.

Hope one of these helps you.

0

精彩评论

暂无评论...
验证码 换一张
取 消