1

I have an array of restaurants that are objects with name, type, location, etc. How can I search for just a restaurant name, or just a location without searching the entire array object?

For example my array with 2 Restaurant objects. I only want to search if Name contains "United". If i search the entire object, both will show up because of the country.

[<Restaurant: 0x7fcb0ad80950, objectId: LA74J92QDA, localId: (null)> {
    City = "New York";
    Country = "United States";
    Name = United Bakery;
    RestaurantLoc = "<PFGeoPoint: 0x7fcb0af6cbc0, latitude: 40.759212, longitude: -73.984632>";
    State = NY;
    Street = "1 main st.";
    Zipcode = 10055;
}, <Restaurant: 0x7fcb0af6cfc0, objectId: 0aKFrpKN46, localId: (null)> {
    City = "New York";
    Country = "United States";
    Name = Wendys Bakery;
    RestaurantLoc = "<PFGeoPoint: 0x7fcb0af6cd60, latitude: 40.759210, longitude: -73.984631>";
    State = NY;
    Street = "1 main st.";
    Zipcode = 10055;
}]

This is the block that creates the array of objects:

            let userGeoPoint = currentLocGeoPoint
            var query = PFQuery(className:"Restaurant")
            query.whereKey("RestaurantLoc", nearGeoPoint:userGeoPoint, withinMiles:50)
            query.limit = 2

            query.findObjectsInBackgroundWithBlock {
                (object: [AnyObject]!, error: NSError!) -> Void in
                if object != nil {
                    println(object)
                } else {
                    println("The getObject request failed with error: \(error)")
                }
            }

2 Answers 2

1

You can use filteredArrayUsingPredicate. Here is the small example:

NSString *searchString = @"a";
NSArray *filteredProducts = [products filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"title CONTAINS[cd] %@", searchString]];

Swift:

let searchString = "text_to_search"
let predicate = NSPredicate(format: "title CONTAINS[cd] %@", searchString);
let filteredArray = array.filteredArrayUsingPredicate(predicate!);
Sign up to request clarification or add additional context in comments.

5 Comments

He said in swift not in Objective - C
@Leena what a problem to use filteredArrayUsingPredicate in swift?
@VitaliyGozhenko I mean to say the question was related to swift so the answer should be in swift instead of Objective-C......
@VitaliyGozhenko Thank you for the example. I tried using filteredArrayUsingPredicate but ran into this error in swift '[AnyObject]' does not have a member named 'filteredArrayUsingPredicate'
@Rachel I think this is because object, which you try to filteredArrayUsingPredicate isn't NSArray object, but something another...
0

Try to use filter(_:) function. From Apple Developer:

Returns an array containing the elements of the array for which a provided closure indicates a match.

Use this method to return a new array by filtering an existing array. The closure that you supply for includeElement: should return a Boolean value to indicate whether an element should be included (true) or excluded (false) from the final collection

Source: https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Array.html

1 Comment

Thanks, i read through it. But can the filter function be used to find if "Name" contains "xxx" or "yyy"? Would you mind providing a short example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.