简体   繁体   中英

Objective-C, Core-Data issue

I use Core-Data in my Iphone application. In my Core-Data I have something like that;

[object:table, number:2]

[object:table, number:4]

[object:window, number:2]

[object:window, number:5]

[object:chair, number:2]

[object:chair, number:3]

How can I get a NSArray with table's lowest number, window's lowest number and chair's lowest number? Thanks.

taken STRAIGHT from the apple website

https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html

1) specify the type of entity you want to get

NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
    entityForName:@"Employee" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];

2) specify the charateristics you are looking for, (you can ignore this since you want all)

// Set example predicate and sort orderings...
NSNumber *minimumSalary = ...;
NSPredicate *predicate = [NSPredicate predicateWithFormat:
    @"(lastName LIKE[c] 'Worsley') AND (salary > %@)", minimumSalary];
[request setPredicate:predicate];

3) specify the order

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
    initWithKey:@"firstName" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];

NSError *error;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil)
{
    // Deal with error...
}

4) you get a sorted array so... either get the last or first entry, depending on the ordering you pick.

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