使用另一个Array过滤数组PFObject

时间:2015-02-08 08:17:55

标签: objective-c swift parse-platform

我有两个[PFObjects]数组。我正在尝试根据array1 restaurantPFObjects的objectID过滤array2 foodPhotoObjectsRestaurantID。但是self.detailsForFoodPhotos.append(item)永远不会被调用。

现行代码:
    var foodPhotoObjectsRestaurantID: [PFObject] = self.foodPhotoObjects.map { $0.objectForKey("RestaurantName") as PFObject }

    for var index = 0 ; index <= foodPhotoObjectsRestaurantID.count - 1 ; index += 1 {

        //self.detailsForFoodPhotos = self.restaurantPFObjects!.filter({$0 == foodPhotoObjectsRestaurantID[index]})
        for item in self.restaurantPFObjects! {
            if (item == foodPhotoObjectsRestaurantID[index]){
                self.detailsForFoodPhotos.append(item)
            }
        }
    }

Array1:foodPhotoObjectsRestaurantID

    [<Restaurant: 0x17411ef90, objectId: 0aKFrpKN46, localId: (null)> {
    }, <Restaurant: 0x17411f0b0, objectId: lZJJHkFmQl, localId: (null)> {
    }, <Restaurant: 0x17411f1d0, objectId: lZJJHkFmQl, localId: (null)> {
    }, <Restaurant: 0x17411f2f0, objectId: lZJJHkFmQl, localId: (null)> {
    }, <Restaurant: 0x17411f410, objectId: lZJJHkFmQl, localId: (null)> {
    }, <Restaurant: 0x17411f530, objectId: 0aKFrpKN46, localId: (null)> {
    }]

Array2:restaurantPFObjects

[<Restaurant: 0x17411a940, objectId: 0aKFrpKN46, localId: (null)> {
    Atmosphere = 609;
    City = "New York";
    CloseHours =     (
        0015,
        2350,
        2350,
        2350,
        2350,
        2350,
        2350
    );
    Closed = 0;
    Country = "United States";
    FairPrice = 209;
    FoodQuality = 900;
    FoodType = Japanese;
    Name = "Le Bec Fin";
    OpenHours =     (
        0011,
        0015,
        0011,
        0011,
        0011,
        0011,
        0011
    );
    OverallDislikes = 250;
    OverallLikes = 1000;
    Phone = "516-507-0982";
    Photo = "<PFFile: 0x174465380>";
    Price = 1;
    Rating = 1;
    RestaurantLoc = "<PFGeoPoint: 0x1742342e0, latitude: 40.759210, longitude: -73.984631>";
    RestaurantType = Lunch;
    Service = 803;
    SiteLink = "http://www.google.com";
    State = NY;
    Street = "900 main st.";
    Zipcode = 10055;
}, <Restaurant: 0x17411ce60, objectId: lZJJHkFmQl, localId: (null)> {
    Atmosphere = 100;
    City = "New York";
    CloseHours =     (
        2350,
        2350,
        2350,
        2350,
        2350,
        2350,
        2350
    );
    Closed = 0;
    Country = "United States";
    FairPrice = 500;
    FoodQuality = 350;
    FoodType = "Japanese, Chinese";
    Name = "Sumo Japan";
    OpenHours =     (
        1215,
        0015,
        0011,
        0011,
        0011,
        0011,
        0011
    );
    OverallDislikes = 400;
    OverallLikes = 500;
    Phone = "888-888-8888";
    Photo = "<PFFile: 0x174465500>";
    Price = 0;
    Rating = 5;
    RestaurantLoc = "<PFGeoPoint: 0x174234420, latitude: 40.759220, longitude: -73.984632>";
    RestaurantType = Breakfast;
    Service = 200;
    SiteLink = "http://www.google.com";
    State = NY;
    Street = "1 main st.";
    Zipcode = 10055;
}, <Restaurant: 0x17411cdd0, objectId: LA74J92QDA, localId: (null)> {
    Atmosphere = 9;
    City = "New York";
    CloseHours =     (
        0015,
        0015,
        0015,
        0015,
        0015,
        0015,
        0015
    );
    Closed = 1;
    Country = "United States";
    FairPrice = 10;
    FoodQuality = 10;
    FoodType = Japanese;
    Name = "Honey Pig";
    OpenHours =     (
        0011,
        0011,
        0011,
        0011,
        0011,
        0011,
        0011
    );
    OverallDislikes = 0;
    OverallLikes = 10;
    Phone = "888-888-8888";
    Photo = "<PFFile: 0x174465440>";
    Price = 2;
    Rating = 3;
    RestaurantLoc = "<PFGeoPoint: 0x174234380, latitude: 40.759212, longitude: -73.984632>";
    RestaurantType = Dinner;
    Service = 9;
    SiteLink = "http://www.google.com";
    State = NY;
    Street = "1 main st.";
    Zipcode = 10055;
}]

1 个答案:

答案 0 :(得分:2)

问题是您正在使用等于运算符,您应该使用相等方法。这可能是比较对象指针,这不是你想要的:

item == foodPhotoObjectsRestaurantID[index]

你应该这样做:

item.objectId == foodPhotoObjectsRestaurantID[index].objectId