我想從.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