我有大约20个实体:Event1 ... Event20 在每个实体中,我有属性值1 ... values20
我需要记录数据,例如Event5 values8
但是出了点问题 - 可能是每个值8中的数据记录(Event1 ... Event20)
怎么做对了?
NSString *str = [NSString stringWithFormat:@"Event%d", variable];
NSString *value = [NSString stringWithFormat:@"values%d", vari];
TermometrAppDelegate *app;
app = (TermometrAppDelegate *)[UIApplication sharedApplication].delegate;
NSFetchRequest *fetchRequests = [[NSFetchRequest alloc] init];
NSEntityDescription *entit = [NSEntityDescription entityForName:str inManagedObjectContext:app.managedObjectContext];
[fetchRequests setEntity:entit] ;
[fetchRequests setPropertiesToFetch :[NSArray arrayWithObject:value]];
[fetchRequests setReturnsDistinctResults:YES];
NSError *error;
NSArray *fetchedObject = [app.managedObjectContext executeFetchRequest:fetchRequests error:&error];
NSManagedObject *fetched = nil;
NSManagedObject *fetch = nil;
printf("\n%d", [fetchedObject count]);
答案 0 :(得分:1)
下面的代码检索Entity5中Value8的值等于someValueForEight.text(UITextField)。这应该可以帮到你。
TermometrAppDelegate *app;
app = (TermometrAppDelegate *)[UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = [app managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Entity5" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(value8 = %@)", someValueForEight.text];
[request setPredicate:pred];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];