【发布时间】:2025-12-09 03:00:01
【问题描述】:
我正在尝试从 .plist 向 CoreData 添加价值并从 coreData 获取并将它们显示在 UITable 上。我从 .plist 中获取并将它们发送到 CoreData 。但是我不能从 CoreData entity 中获取它们并设置为一些 NSMutableArray .. 这是我的代码
编辑:已解决
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 语句的值。为什么不添加更多调试语句并向我们展示值。
-
我没有收到任何错误。问题是我不知道如何将 CoreData 中的值设置为我的 personList .. 我知道如何添加到数组中,例如 [personList addObject:[NSString stringWithFormat:@"SAMPLE" ]];但是我将如何将 CoreData 的价值添加到 personList 我无法弄清楚。
-
如果您已解决此问题,请将其作为答案发布并接受答案,以便显示问题已得到解答。
标签: iphone uitableview core-data