2010-03-28 5 views
0

J'essaie d'ajouter de la valeur à CoreData à partir de .plist et d'obtenir de coreData et de les montrer sur UITable. Je prends de .plist fis et les envoyer à CoreData .Mais je ne peux pas les prendre de CoreData entity et mettre une certaine NSMutableArray .. Voici mes codesobtenir un enregistrement de CoreData?

Edit: SOLVED

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]]; 


} 

Voici ma tableView fonction

- (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; 
} 
+1

Vous ne l'avez pas mentionné quelles erreurs vous » voir ou la valeur des instructions NSLog que vous avez incluses. Pourquoi ne pas ajouter d'autres instructions de débogage et nous montrer les valeurs. – Jordan

+0

Je ne reçois aucune erreur.Problem est je ne sais pas comment définir la valeur de CoreData à mon personList .. Je sais comment ajouter au tableau comme [personList addObject: [NSString stringWithFormat: @ "SAMPLE"]]; Mais comment vais-je ajouter de la valeur de CoreData à personList je ne peux pas le comprendre. – Ercan

+0

Si vous avez résolu ce problème, postez-le comme réponse et acceptez la réponse pour que la réponse à la question soit affichée. –

Répondre

0

Voici mon solution.I créé un tableau NSMutable et setPeopleList utilisé pour ajouter des valeurs de fetchedResultsController fetchedObjects.Also à utiliser s et l'opération que je crée mon tableau dans le fichier d'en-tête et ajouté

@property (nonatomic, retain) NSMutableArray *peopleList; 

et dans le fichier .m i créé

@synthesize peopleList; 

et j'utilisé

[self setPeopleList:(NSMutableArray *)[fetchedResultsController 
            fetchedObjects]]; 
+2

'NSFetchedResultsController' renvoie un' NSArray' et non un 'NSMutableArray'. Lancer ce retour est dangereux au mieux. Il vaut mieux créer un '-mutableCopy' du tableau retourné pour garantir qu'il est en fait mutable. –

Questions connexes