Note that there are some explanatory texts on larger screens.

plurals
  1. PONot Able to Retrieve data using core data
    text
    copied!<p>I am using coredata in my application to store data.i have to add the data in one view controller and retrieve it in another view controller.i tried the following code but it is not working.</p> <pre><code>//addViewController.m -(IBAction)save:(id)sender { CoreDataOneAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSManagedObject *newContact; newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:context]; [newContact setValue:name.text forKey:@"name"]; [newContact setValue:amount.text forKey:@"amount"]; name.text = @""; amount.text = @""; //label status.text = @"saved"; NSError *error; [context save:&amp;error]; } </code></pre> <p>I want to retrieve the values and display them in a tableView</p> <pre><code>//retrieveViewController.m - (void)viewDidLoad { objects = [[NSArray alloc]init]; CoreDataOneAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entityDesc]; NSError *error; objects = [context executeFetchRequest:request error:&amp;error]; [request release]; [super viewDidLoad]; } //tableView -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [objects count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { labelOne = [[UILabel alloc]initWithFrame:CGRectMake(5, 11, 110, 21)]; labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(230, 11, 70, 21)]; static NSString *cellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier]autorelease]; } [cell.contentView addSubview:labelTwo]; [cell.contentView addSubview:labelOne]; NSManagedObject *matches = nil; matches = [objects objectAtIndex:indexPath.row]; NSString *str1=[NSString stringWithFormat:@"%@",[matches valueForKey:@"name"]]; labelOne.text = str1; NSString *str2=[NSString stringWithFormat:@"%@",[matches valueForKey:@"amount"]]; labelTwo.text = str2; return cell; } </code></pre> <p>I am getting EXC_BAD_ACCESS error.i tried using NSZombieEnabled and i got the following error.</p> <pre><code>2012-04-27 11:59:18.153 CoreDataOne[4370:207] *** -[_PFArray objectAtIndex:]: message sent to deallocated instance 0x5931e40 </code></pre> <p>i am able to retrieve the values if write the code what i have written in viewDidLoad in cellForRowAtIndexPath but how to declare the numberOfRows.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload