objective-c - 用另一个数组过滤数组 PFObjects

标签 objective-c swift parse-platform

我有两个 [PFObjects] 数组。我正在尝试根据 array1 foodPhotoObjectsRestaurantID 的 objectID 过滤 array2 restaurantPFObjects。但是 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)> {
    }]

数组2:餐厅PF对象

[<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;
}]

最佳答案

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

item == foodPhotoObjectsRestaurantID[index]

你应该这样做:

item.objectId == foodPhotoObjectsRestaurantID[index].objectId

关于objective-c - 用另一个数组过滤数组 PFObjects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28391960/

相关文章:

ios - 联系人信息样式 TableView

iphone - NSURLRequest 设置 HTTP header

ios - 获取数据时返回相同的 YouTube 视频

ios - PF用户丢失数据

ios - 从 NsMutable 数组而不是字符串加载按钮值

iphone - 在 iPhone 应用程序中使用 Google Reader API 和 OAuth

ios - 未在 UITableView 中调用 scrollviewDidEndDragging

ios - 通过 WhatsApp 共享 PDF

ios - 通过 createdAt 快速排序解析结果

ios - 如何在Parse iOS中避免数据重复?