我想从.plist中增加CoreData的值,并从coreData获取并在UITable上显示它们。我做了采取从的.plist并将它们发送到CoreData。但我不能把他们从CoreData entity
,并设置一些NSMutableArray
。这里我的代码从CoreData获取记录?
编辑:解决
NSString *path = [[NSBundle mainBundle] pathForResource:@"FakeData"
ofType:@"plist"];
NSArray *myArray=[[NSArray alloc] initWithContentsOfFile:path];
FlickrFetcher *flickrfetcher =[FlickrFetcher sharedInstance];
managedObjectContext =[flickrfetcher managedObjectContext];
for(NSDictionary *dict in myArray){
Photo *newPhoto = (Photo *)[NSEntityDescription
insertNewObjectForEntityForName:@"Photo"
inManagedObjectContext:managedObjectContext];
// set the attributes for the photo
NSLog(@"Creating Photo: %@", [dict objectForKey:@"name"]);
[newPhoto setName:[dict objectForKey:@"name"]];
[newPhoto setPath:[dict objectForKey:@"path"]];
NSLog(@"Person is: %@", [dict objectForKey:@"user"]);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ==%@",[dict objectForKey:@"user"]];
NSMutableArray *peopleArray = (NSMutableArray *)[flickrfetcher
fetchManagedObjectsForEntity:@"Person" withPredicate:predicate];
NSEnumerator *enumerator = [peopleArray objectEnumerator];
Person *person;
BOOL exists = FALSE;
while (person = [enumerator nextObject]) {
NSLog(@" Person is: %@ ", person.name);
if([person.name isEqualToString:[dict objectForKey:@"user"]]) {
exists = TRUE;
NSLog(@"-- Person Exists : %@--", person.name);
[newPhoto setPerson:person];
}
}
// if person does not already exist then add the person
if (!exists) {
Person *newPerson = (Person *)[NSEntityDescription
insertNewObjectForEntityForName:@"Person"
inManagedObjectContext:managedObjectContext];
// create the person
[newPerson setName:[dict objectForKey:@"user"]];
NSLog(@"-- Person Created : %@--", newPerson.name);
// associate the person with the photo
[newPhoto setPerson:newPerson];
}
//THis IS myArray that I want to save value in it and them show them in UITABLE
personList=[[NSMutableArray alloc]init];
NSLog(@"lllll %@",[personList objectAtIndex:0]);
// save the data
NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1);
}
}
//To access core data objects you can try the following:
// setup our fetchedResultsController
fetchedResultsController = [flickrfetcher fetchedResultsControllerForEntity:@"Person" withPredicate:nil];
// execute the fetch
NSError *error;
BOOL success = [fetchedResultsController performFetch:&error];
if (!success) {
// Handle the error.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1);
}
// save our data //HERE PROBLEM THAT I DONW KNOW WHERE IT IS SAVING
//[personList addObject:(NSMutableArray *)[fetchedResultsController fetchedObjects]];
[self setPersonList:(NSMutableArray *)[fetchedResultsController
fetchedObjects]];
}
这里我tableView
功能
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"ss %d",[personList count]);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[personList objectAtIndex:indexPath.row];
// Set up the cell...
return cell;
}
您还没有提到什么错误,你”重新看到你所包含的NSLog语句的值。为什么不添加更多的调试语句并向我们显示值。 – Jordan 2010-03-28 22:52:08
我没有得到任何错误。问题是我不知道如何设置值从CoreData到我的personList ..我知道如何添加到数组像[personList addObject:[NSString stringWithFormat:@“SAMPLE”]]; 但是,我如何将CoreData的价值添加到personList我无法弄清楚。 – Ercan 2010-03-28 22:58:41
如果你已经解决了这个问题,然后将它作为答案发布,并接受答案,以便问题得到解答。 – 2010-03-30 15:17:43