从NSFetchResultsController的NSObject属性创建数组 [英] Create Array From Attribute of NSObject From NSFetchResultsController
本文介绍了从NSFetchResultsController的NSObject属性创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个可提取NSObject的NSFetchResultsController.
I have a NSFetchResultsController that fetches a NSObject.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"WeekDay" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate: predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
通常,我会这样获取数据:
Usually I would get the data from this fetch like this:
self.selectedWeekDay = (WeekDay *)[fetchedResultsController objectAtIndexPath:indexPath];
但是我需要的是一个带有所有timeStamps的数组,格式类似于
But what I need is an array with all the timeStamps, in a format similar to
[NSArray arrayWithObjects: @"2011-11-01 00:00:00 +0000", @"2011-12-01 00:00:00 +0000", nil];
我该怎么做?
当我NSLog您提供的数组代码时,它给出以下信息:
When I NSLog the array code you provided, it give this:
The content of data is(
{
timeStamp = "2011-06-14 21:30:03 +0000";
},
{
timeStamp = "2011-06-16 21:00:18 +0000";
},
{
timeStamp = "2011-06-11 21:00:18 +0000";
},
{
timeStamp = "2011-06-23 19:53:35 +0000";
},
{
timeStamp = "2011-06-21 19:53:35 +0000";
}
)
Tapuko图书馆日历代码:
Tapuko Library Calendar Code:
- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate {
NSLog(@"calendarMonthView marksFromDate toDate");
NSLog(@"Make sure to update 'data' variable to pull from CoreData, website, User Defaults, or some other source.");
// When testing initially you will have to update the dates in this array so they are visible at the
// time frame you are testing the code.
NSArray *data = [NSArray arrayWithObjects:
@"2011-01-01 00:00:00 +0000", @"2011-12-01 00:00:00 +0000", nil];
// Initialise empty marks array, this will be populated with TRUE/FALSE in order for each day a marker should be placed on.
NSMutableArray *marks = [NSMutableArray array];
// Initialise calendar to current type and set the timezone to never have daylight saving
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Construct DateComponents based on startDate so the iterating date can be created.
// Its massively important to do this assigning via the NSCalendar and NSDateComponents because of daylight saving has been removed
// with the timezone that was set above. If you just used "startDate" directly (ie, NSDate *date = startDate;) as the first
// iterating date then times would go up and down based on daylight savings.
NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit |
NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit)
fromDate:startDate];
NSDate *d = [cal dateFromComponents:comp];
// Init offset components to increment days in the loop by one each time
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
// for each date between start date and end date check if they exist in the data array
while (YES) {
// Is the date beyond the last date? If so, exit the loop.
// NSOrderedDescending = the left value is greater than the right
if ([d compare:lastDate] == NSOrderedDescending) {
break;
}
// If the date is in the data array, add it to the marks array, else don't
if ([data containsObject:[d description]]) {
[marks addObject:[NSNumber numberWithBool:YES]];
} else {
[marks addObject:[NSNumber numberWithBool:NO]];
}
// Increment day using offset components (ie, 1 day in this instance)
d = [cal dateByAddingComponents:offsetComponents toDate:d options:0];
}
[offsetComponents release];
return [NSArray arrayWithArray:marks];
}
推荐答案
Core Data Snippets指南正是这种情况,标题为.
The Core Data Snippets guide has exactly this scenario, titled 'Fetching specific property values'.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:NO]; //set to YES if you only want unique values of the property
[request setPropertiesToFetch :[NSArray arrayWithObject:@"timeStamp"]]; //name(s) of properties you want to fetch
// Execute the fetch.
NSError *error;
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
这篇关于从NSFetchResultsController的NSObject属性创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文