从核心数据数组对象创建新数组?

时间:2015-02-18 12:39:26

标签: ios objective-c core-data

我目前正在开发Core Data,我有以下代码

NSManagedObjectContext *managedObjContxt = [self managedObjectContext];
NSFetchRequest *fetchReq = [[NSFetchRequest alloc] initWithEntityName:@"BDEvent"];
NSMutableArray *fetchedObj= [[managedObjContxt executeFetchRequest:fetchReq error:nil] mutableCopy];

现在我的fetchedObj数组包含

<_PFArray 0x7b78d4e0>(
<BDEvent: 0x7b78cf40> (entity: BDEvent; id: 0x7b78be60 <x-coredata://066794C5-FA98-4D76-B306-C62CF65563F2/BDEvent/p1> ; data: {
    name = rock;
    status = "D1";
    timestamp = "18-02-2015 12:10:10";
}),
<BDEvent: 0x7b78d1f0> (entity: BDEvent; id: 0x7b78be70 <x-coredata://066794C5-FA98-4D76-B306-C62CF65563F2/BDEvent/p2> ; data: {
    name = Illa;
    status = "D1";
    timestamp = "18-02-2015 12:15:01";
}),
<BDEvent: 0x7b78d290> (entity: BDEvent; id: 0x7b78be80 <x-coredata://066794C5-FA98-4D76-B306-C62CF65563F2/BDEvent/p3> ; data: {
    name = john;
    status = "EXIT|D2";
    timestamp = "18-02-2015 12:25:05";
})
)

现在我想基于状态

创建2个数组

Array1将包含没有EXIT状态的时间戳值,而Array2将包含具有EXIT状态的时间戳值。

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

要获取带有“退出”代码的数组,请在您的获取请求中添加一个谓词。

    NSManagedObjectContext *managedObjContxt = [self managedObjectContext];
    NSFetchRequest *fetchReq = [[NSFetchRequest alloc] initWithEntityName:@"BDEvent"];

//add this line
    fetchReq.predicate = [NSPredicate predicateWithFormat:@"status contains 'EXIT'"];


    NSMutableArray *fetchedObj= [[managedObjContxt executeFetchRequest:fetchReq error:nil] mutableCopy];

获取没有'EXIT'代码的数组,使用以下谓词,

  fetchReq.predicate = [NSPredicate predicateWithFormat:@"NOT (status contains 'EXIT')"];