【问题标题】:Filter Array PFObjects with another Array用另一个数组过滤对象数组
【发布时间】:2015-02-08 08:17:54
【问题描述】:

我有两个 [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)> {
    }]

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

【问题讨论】:

    标签: objective-c swift parse-platform


    【解决方案1】:

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

    item == foodPhotoObjectsRestaurantID[index]
    

    你应该这样做:

    item.objectId == foodPhotoObjectsRestaurantID[index].objectId
    

    【讨论】:

    • 两者都是 PFObjects。比较应该是有效的
    • 看看数组。 PFObjects 不是相同的指针,即使 objectIds 相同。因此你不能比较指针,你必须比较 objectIds。
    • String does not have member named isEqualToString 我应该将整个数组转换为任何对象吗?
    • objectIds可能相同,但对象本身在内存中的位置不同,因此它们的指针也不相同。 object1 == object2 比较指针。 object1.objectId == object2.objectId 做你想做的事。 :-)
    • @Emma 您可以在 Swift 中自定义相等中缀运算符,以便在比较两个 PFObjects 时自动测试 objectIds。请参阅此处的“等价运算符”:developer.apple.com/library/prerelease/ios/documentation/Swift/…
    猜你喜欢
    • 2018-10-03
    • 2021-05-14
    • 1970-01-01
    • 2021-10-16
    • 2019-05-05
    • 1970-01-01
    相关资源
    最近更新 更多