จะกรองอาร์เรย์โดยใช้อาร์เรย์ได้อย่างไร?

ฉันมีคลาสแบบกำหนดเองสามประเภท (ดูด้านล่าง) และสามอาร์เรย์componentList,componentGroupListและcomponentGroupItemList อาร์เรย์ไม่ได้เชื่อมโยง แต่ละอาร์เรย์มีออบเจ็กต์ทั้งหมด ฉันต้องกรองส่วนประกอบเฉพาะ กลุ่มที่เกี่ยวข้องทั้งหมด และรายการที่เกี่ยวข้องทั้งหมด

ตอนนี้ฉันรู้วิธีกรองส่วนประกอบรายการโดยใช้ @"componentId==123" และรับวัตถุส่วนประกอบที่ต้องการ ฉันยังสามารถกรองกลุ่มจาก ComponentGroupList โดยใช้เพรดิเคตเดียวกันได้ เนื่องจากออบเจ็กต์ ComponentGroup มีคีย์ ComponentId เดียวกัน อย่างไรก็ตาม ฉันไม่รู้วิธีกรองออบเจ็กต์ ComponentGroupItem ที่เกี่ยวข้องจาก ComponentGroupItemList

ขณะนี้ ฉันได้กรองอาร์เรย์ที่มีออบเจ็กต์ ComponentGroup แล้ว และฉันต้องการกรองส่วนประกอบGroupItemList โดยใช้อาร์เรย์นั้น เป็นไปได้หรือฉันต้องแยกค่า "groupId" ทั้งหมดจาก filteredComponentGroupList ลงในสตริงแล้วสร้างเพรดิเคต

ชั้นเรียน:

@interface Component : NSObject

  @property (nonatomic, strong) NSNumber *componentId;
  @property (nonatomic, strong) NSString *title;

@end

@interface ComponentGroup : NSObject

  @property (nonatomic, strong) NSNumber *groupId;
  @property (nonatomic, strong) NSNumber *componentId;
  @property (nonatomic, strong) NSString *title;

@end

@interface ComponentGroupItem : NSObject

  @property (nonatomic, strong) NSNumber *itemId;
  @property (nonatomic, strong) NSNumber *groupId;
  @property (nonatomic, strong) NSString *title;

@end

person Centurion    schedule 07.05.2013    source แหล่งที่มา


คำตอบ (2)


คุณต้องแยกรหัสกลุ่มก่อน

NSArray *groupIds = [filteredComponentGroupList valueForKey:@"groupId"];

และใช้สิ่งนั้นเป็นภาคแสดง

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"groupId IN %@", groupIds];
NSArray *filteredComponentGroupItemList = [componentGroupItemList filteredArrayUsingPredicate:predicate];
person Martin R    schedule 07.05.2013

เมื่อมองแวบแรก โครงสร้างข้อมูลของคุณดูเหมือนจะซ้ำซ้อนเล็กน้อย แต่ฉันเดาว่าคุณคงคิดผ่านแล้ว

หากฉันเข้าใจข้อกำหนดของคุณอย่างถูกต้อง แสดงว่าคุณกรองอาร์เรย์ของกลุ่มส่วนประกอบแล้ว (เรียกว่า filteredComponentGroups) และคุณต้องการกรองอาร์เรย์อื่น (componentGroupItemList) โดยใช้ filteredComponentGroups

ในกรณีนั้น คุณสามารถใช้ตัวดำเนินการ IN ของ NSPredicate และสร้างอาร์เรย์ของ ID โดยมี valueForKey: บนอาร์เรย์ได้ valueForKey: บนอาร์เรย์จะสร้างอาร์เรย์ใหม่โดยมีค่าเฉพาะของคีย์นั้นของแต่ละอ็อบเจ็กต์ในคอลเลกชันดั้งเดิม ทรงพลังมากสำหรับสถานการณ์เช่นนี้

 NSArray *filteredComponentGroups = // ... your filtered components

 NSArray *componentIdsFromFilteredComponentGroups = [filteredComponentGroups valueForKey: @"groupId"];
 NSPredicate *inFilteredComponentGroupsP = [NSPredicate predicateWithFormat: @"groupId IN %@", componentIdsFromFilteredComponentGroups];

 NSArray *filteredGroupItemList = [componentGroupItemList filteredArrayUsingPredicate: inFilteredComponentGroupsP];

พิมพ์โดยตรงในเบราว์เซอร์ ดังนั้นระวังการพิมพ์ผิด

person Monolo    schedule 07.05.2013
comment
ตอนนี้คุณเร็วขึ้น 8 วินาที :-) - ถ้าฉันเข้าใจถูกต้อง คีย์ที่จำเป็นคือ groupId ไม่ใช่ componentId แต่ฉันอาจผิด - person Martin R; 07.05.2013