Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

NSArray Loop to find NSString?

Options
  • 21-12-2010 6:47pm
    #1
    Registered Users Posts: 8,004 ✭✭✭


    Hi Folks,

    I have a NSArray. It contains a number of elements, the number of elements can vary. Each element looks like this:
    {
            address = "Dublin";
            name = "John";
            dayofweek = "Tuesday";
            
        }
    

    I need to loop through the whole array to find a certain string i.e. Each element that contains "John" or "Tuesday" I can't seem to do it. I've tried a few implementations but to no avail. The data will be unique i.e. "Tuesday" will always appear like that, so I don't need massive filtering power.

    When I find a match, I want to put the whole array element in a NSMutableArray e.g.
    [filteredResults addObject:item]

    Where item is the element etc.

    Any help would be greatly appreciated.

    ironclaw


Comments

  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    You are looking for Predicates.
    I won't go into what they are here in detail, but have a look when you get a chance.
    Link Apple Developer

    All you need to do is declare your predicate
    NSPredicate * myLittlePredicate = [NSPredicate predicateWithFormat:@"(address == '123 Fake Street')"];
    
    Then apply it to your array.
    NSArray * Result = [myLittleNSArray filteredArrayUsingPredicate:myLittlePredicate];
    

    The array Result will be populated with the elements of myLittleNSArray that have an address that is an NSString @123 Fake Street

    You can set the value of the predicate like an If statement ( use ==, ||, && and !=) and use multiple arguments in one predicate.
    NSPredicate * myLittlePredicate = [NSPredicate predicateWithFormat:@"(address == '123 Fake Street' || address != 'Someplace' && name != 'Yore Ma')"];
    

    Some Regex too I believe.


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    Thanks Genghiz Cohen. I'll take it for a spin tomorrow, see what happens. Will that find the info even though an element has all the different data in in?


  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    ironclaw wrote:
    Will that find the info even though an element has all the different data in in?
    I don't understand what you mean.

    If you mean the elements will be different classes you can filter the array first.
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    for(int i = 0; i < [myLittleNSArray count]li++)
    {
          if([[myLittleNSArray objectAtIndex:i] isKindOfClass:[someClass Class]])
                   [temp addObject:[myLittleNSArray objectAtIndex:i]];//You could preform a check against the address member here too, but predicates are nice to learn
    }
    //Apply predicate to temp array
    


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    Works perfectly! Thanks Genghiz Cohen. Probably the simplest solution I've been given.


  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    ironclaw wrote: »
    Works perfectly! Thanks Genghiz Cohen. Probably the simplest solution I've been given.

    I <3 Predicates


  • Advertisement
  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    Another challenge for you :)

    If I had the following elements:
    {
            address = "Stillorgan, Dublin";
            name = "John Bloggs";
            dayofweek = "Tuesday";
            
        }
    
      {
            address = "Foxrock, Dublin";
            name = "John Smith";
            dayofweek = "Tuesday";
            
        }
    
     {
            address = "Milltown, Cork";
            name = "John Smith";
            dayofweek = "Tuesday";
            
        }
    

    Is there anyway to use a like predicate? For instance, I want to check for all the address's that are like "dublin" or "dub" etc So as above, the elements for John Smith and John Bloggs who live in Dublin would be returned? Or as another example, find "foxrock" or "rock" so only the John Smith from Foxrock element would be returned?


  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    I'll have a look at this tonight after work. (If I can drag myself away from Shattered Horizons)

    In the meantime you could mess around with the LIKE and CONTAINS evaluators.
    NSPredicate * myLittlePredicate = [NSPredicate predicateWithFormat:@"(address CONTAINS 'Dub')"];
    
    Which would return all elements where the address contains dub, E.G. Dublin, Glendub, Dubai and so on.
    I'm not sure how LIKE works yet.

    Sometimes you'll see CONTAINS[c] or [cd], the c simply means the search is case sensitive (A != a, obviously :p), the d means it will look at fadas and other character marks like Umlauts (so, á != a).


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    I'll have a look at this tonight after work. (If I can drag myself away from Shattered Horizons)

    In the meantime you could mess around with the LIKE and CONTAINS evaluators.
    NSPredicate * myLittlePredicate = [NSPredicate predicateWithFormat:@"(address CONTAINS 'Dub')"];
    
    Which would return all elements where the address contains dub, E.G. Dublin, Glendub, Dubai and so on.
    I'm not sure how LIKE works yet.

    Sometimes you'll see CONTAINS[c] or [cd], the c simply means the search is case sensitive (A != a, obviously :p), the d means it will look at fadas and other character marks like Umlauts (so, á != a).

    This works perfectly! In case anyone wants to use a dynamic CONTAINS. I use this:
    NSString *searchText = searchBar.text; //From search bar
    
    //textForFilter is my NSPredicate	
    textForFilter = [NSPredicate predicateWithFormat:@"(address CONTAINS %@ )", searchText];
    
    //Apply for Array.
    


Advertisement