Filter Array of [AnyObject] in Swift

前端 未结 5 880
借酒劲吻你
借酒劲吻你 2020-12-05 10:25

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

相关标签:
5条回答
  • 2020-12-05 10:43

    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.

    0 讨论(0)
  • 2020-12-05 10:43

    I have a solution as given below.

    func filterByCuisineType(list: [Restaurant]) -> [Restaurant]{
    
        if self.cuisineTypes.count == 0 {
            return list
        }
    
        let array: [Restaurant] =  list.filter { (restaurant) -> Bool in
    
            for cuisineName in self.cuisineTypes{
    
                let isContained: Bool = restaurant.cuisineType.contains(cuisineName)
    
                if isContained {
                    return true
                }
            }
    
            return false
        }
    
        return array
    }
    
    0 讨论(0)
  • 2020-12-05 10:44

    Swift 3 Solution

    Use the filter method on an array.

    let restaurants: [Restaurants] = [...]
    restaurants.filter({(restaurant) in
        return Bool(restaurant.type == "sushi")
    })
    

    or return Bool(restaurant.type.contains("sushi")) if type is an array.

    0 讨论(0)
  • 2020-12-05 10:45

    Ok, if the array objects contains only Restaurant(s) the following code does work.

    Lets say Restaurant is something like this:

    enum RestaurantType {
        case Sushi, Japanese, Asian
    }
    
    class Restaurant {
        var type = [RestaurantType]()
        // more properties here...
    }
    

    First of all lets define an array of Restaurant(s).

    var restaurants = objects as [Restaurant]
    

    Then we can filter it:

    var sushiRestaurants = restaurants.filter { (restaurant : Restaurant) -> Bool in
        return contains(restaurant.type, .Sushi)
    }
    

    Update: Now I am assuming objects is an array of PFObject(s) Just ignore my previous code and try this:

    var restaurants = objects as [PFObject]
    var sushiRestaurants = restaurants.filter { (restaurant : PFObject) -> Bool in
        return contains(restaurant["Type"], "Sushi")
    }
    

    Maybe it will crash again, the problem is that I don't know the type of Restaurant.Type. I'm trying. Maybe the next error message will provide more useful info.

    0 讨论(0)
  • 2020-12-05 10:57

    Modification of Rob's answer as Swift 2.0, In swift 2.0 using Rob's code gives error as follows -

    initializer for conditional binding must have optional type, not 'string'
    

    However it can be solved by using guard statement instead of if-let as below -

    let filteredArray = objects.filter() {
                guard let type = ($0 as PFObject)["Type"] as String else {
                    return false
                } 
                return type.rangeOfString("Sushi") != nil
            }
    
    0 讨论(0)
提交回复
热议问题