Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Array of [AnyObject] in Swift

I have an array of AnyObject objects in Swift. Each object has attributes of a restaurant, such as name, type, loc, etc. How can I filter the array if I want to keep all objects in the array that contain type: "Sushi".

Sample array of [AnyObject] with 2 objects. The filter should keep the first object (type: sushi):

[<Restaurant: 0x7ff302c8a4e0, objectId: LA74J92QDA, localId: (null)> {     City = "New York";     Country = "United States";     Name = Sumo Japan;     Type = Sushi, Japanese, Asian; }, <Restaurant: 0x7ff302daa790, objectId: 0aKFrpKN46, localId: (null)> {     City = "New York";     Country = "United States";     Name = Little Italy;     Type = Italian, Pizza; }] 

Current Code (but I'm not sure if the filter can search through an array of [AnyObject]) :

var query = PFQuery(className:"Restaurant") query.whereKey("RestaurantLoc", nearGeoPoint:userGeoPoint, withinMiles:50) query.limit = 2 query.findObjectsInBackgroundWithBlock {     (objects: [AnyObject]!, error: NSError!) -> Void in     if objects != nil {         println("list of objects of nearby")         println(objects)         let searchString = "Sushi"         let predicate = NSPredicate(format: "Type CONTAINS[cd] %@", searchString);          //Line below gives error: '[AnyObject]' does not have a member named 'filteredArrayUsingPredicate'         //let filteredArray = objects.filteredArrayUsingPredicate(predicate!) 
like image 316
Onichan Avatar asked Dec 30 '14 17:12

Onichan


People also ask

What is Array object in Swift?

Swift Array Initialization An array is a data type designed specifically to hold multiple values in a single ordered collection. An array, for example, could be created to store a list of String values. Strictly speaking, a single Swift based array is only able to store values that are of the same type.


1 Answers

Your array, objects, is an array of PFObject objects. Thus, to filter the array, you might do something like:

let filteredArray = objects.filter() {     if let type = ($0 as PFObject)["Type"] as String {         return type.rangeOfString("Sushi") != nil     } else {         return false     } } 

My original answer, based upon an assumption that we were dealing with custom Restaurant objects, is below:


You can use the filter method.

Let's assume Restaurant was defined as follows:

class Restaurant {     var city: String     var name: String     var country: String     var type: [String]!      init (city: String, name: String, country: String, type: [String]!) {         ...     } } 

So, assuming that type is an array of strings, you'd do something like:

let filteredArray = objects.filter() {contains(($0 as Restaurant).type, "Sushi")} 

If your array of types could be nil, you'd do a conditional unwrapping of it:

let filteredArray = objects.filter() {     if let type = ($0 as Restaurant).type as [String]! {         return contains(type, "Sushi")     } else {         return false     } } 

The particulars will vary a little depending upon your declaration of Restaurant, which you haven't shared with us, but hopefully this illustrates the idea.

like image 79
Rob Avatar answered Nov 03 '22 00:11

Rob