我有一组核心数据对象,它们具有时间(日期)属性。
我想要做的是将具有相同时间的所有核心数据对象放入单独的数组中,然后创建这些数组的数组。如果没有其他对象具有相同的时间,那么该对象将自己在数组中。
即如果核心数据对象数组有3个时间为08:00的对象和1个时间属性为09:00的对象,那么我想创建一个包含前3个对象的数组(时间为08:00)和一个单独的数组与最后一个对象(时间09:00)。然后我想创建一个应该很容易的数组的数组。 Start: [8,8,8,9] Finish: [[8,8,8][9]]
我最挣扎的是如何遍历原始数组并同时拉出所有对象,然后将这些对象放在自己的数组中。
答案 0 :(得分:0)
你可以这样做:
实施可以是这样的:
MyObject *myObject1 = [[MyObject alloc] initWithDate:[NSDate dateWithTimeIntervalSinceNow:10*60]];
MyObject *myObject2 = [[MyObject alloc] initWithDate:[NSDate dateWithTimeIntervalSinceNow:10*60]];
MyObject *myObject3 = [[MyObject alloc] initWithDate:[NSDate dateWithTimeIntervalSinceNow:180*60]];
MyObject *myObject4 = [[MyObject alloc] initWithDate:[NSDate dateWithTimeIntervalSinceNow:120*60]];
MyObject *myObject5 = [[MyObject alloc] initWithDate:[NSDate dateWithTimeIntervalSinceNow:120*60]];
NSMutableArray *myObjects = [NSMutableArray arrayWithObjects:myObject1,myObject2,myObject3,myObject4,myObject5, nil];
NSArray *distinctObjects = [myObjects valueForKeyPath:@"@distinctUnionOfObjects.transientDate"];
NSMutableArray *finalArray = [NSMutableArray array];
for(NSDate *aDate in distinctObjects) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"transientDate == %@", aDate];
NSArray *filteredArray = [myObjects filteredArrayUsingPredicate:predicate];
[finalArray addObject:filteredArray];
}
NSLog(@"distintobjects: %@", finalArray);
以下是MyObject
的样子:
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
{
}
@property(nonatomic, strong) NSDate *myDate;
@property(nonatomic, strong) NSDate *transientDate;
- (id)initWithDate:(NSDate *)aDate;
@end
#import "MyObject.h"
@implementation MyObject
- (id)initWithDate:(NSDate *)aDate {
self = [super init];
self.myDate = aDate;
return self;
}
- (NSDate *)transientDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH"];
return [dateFormatter dateFromString:[dateFormatter stringFromDate:self.myDate]];
}
- (NSString *)description
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
return [NSString stringWithFormat:@"transientDate: %@", [dateFormatter stringFromDate:self.transientDate]];
}
@end
答案 1 :(得分:0)
执行此操作的许多方法之一是使用NSDictionary
对象,其中时间是键,对应的对象是具有此时间的核心数据对象的数组。核心数据对象迭代一次。
NSArray *data = core data objects
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (ManagedObjectClass *object in data) {
NSDate *time = object.time;
// convert time to time without date
NSMutableArray *array = dictionary[time];
if (!array) {
array = [NSMutableArray array];
dictionary[time] = array;
}
[array addObject:object];
}
NSArray *array = dictionary.allValues;
如果时间不需要转换,另一种方法是执行此操作:按时间对核心数据对象进行排序,迭代对象,在时间更改时启动新的子数组并将对象添加到子数组中。
NSArray *data = core data objects
NSArray *sortedData = [data sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"time" ascending:YES]]];
NSMutableArray *array = [NSMutableArray array];
NSMutableArray *subarray = nil;
NSDate *prevTime = nil;
for (ManagedObjectClass *object in sortedData) {
NSDate *time = object.time;
if (![time isEqualToDate:prevTime]) {
subarray = [NSMutableArray array];
[array addObject:subarray];
}
[subarray addObject:object];
prevTime = time;
}