0

I have two arrays of [PFObjects]. I am trying to filter array2 restaurantPFObjects based on the objectIDs of array1 foodPhotoObjectsRestaurantID. However self.detailsForFoodPhotos.append(item) never gets called.

Current Code:
    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;
}]
0

1 Answer 1

2

The problem is that you are using the equality operator where you should be using an equality method. This is probably comparing object pointers, which is not what you want:

item == foodPhotoObjectsRestaurantID[index]

You should be doing something like this:

item.objectId == foodPhotoObjectsRestaurantID[index].objectId
Sign up to request clarification or add additional context in comments.

7 Comments

Both are PFObjects. The comparison should be valid
Take a look at the arrays. The PFObjects are not the same pointers, even if the objectIds are the same. Thus you can't compare pointers, you have to compare objectIds.
String does not have member named isEqualToString Should i convert the entire array into anyobject?
The objectIds may be the same, but the objects themselves live in different places in memory, and therefore their pointers are not the same. object1 == object2 compares pointers. object1.objectId == object2.objectId does what you want. :-)
@Emma you can customize the equality infix operator in Swift to test for objectIds automatically when comparing two PFObjects. See "Equivalence Operators" here: developer.apple.com/library/prerelease/ios/documentation/Swift/…
|

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.