简体   繁体   中英

show to sort coredata with date

I am haing a Simple Iphone based apllication.In that Application coredata is is used for storing data.

I have two fields. one Field is Mesage and other field is date.I want to take the mesage which is sent within last 10 days. I know to this code is used for that..but how to implement this?????

sortedArray = [unsortedArray sortedArrayUsingFunction:sortByStringDate context:NULL]

how to write a query and how to implement this...Anybody help me to implement this.....Advance thanks

如果要按该数组中对象的属性date对数组进行排序,可以编写以下调用:

sortedArray = [unsortedArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]]];

Do something like this:

// Calculate NSDate for 10 days ago
NSDate *now = [NSDate date]; // get current date
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.day = -10;
NSDate *tenDaysAgo = [calendar dateByAddingComponents:dateComponents toDate:now options:0];
[dateComponents release];

// Create NSFetchRequest
NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];
req.predicate = [NSPredicate predicateWithFormat:@"date >= %@", tenDaysAgo];
// Optionally add a sort descriptor too

// Execute fetch request
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:req error:&error];

If this doesn't make sense to you then read about NSFetchRequests and Predicates in the Core Data Programming Guide .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM