0

当我尝试更新我的核心数据对象(空错误)时,我遇到了一个奇怪的错误。我第一次保存对象(餐厅)时,它保存得很好。但是,当我尝试更新餐厅时 - 这就是我遇到错误的地方:

RestaurantDetailViewController.h :

@class Restaurant;

@interface RestaurantDetailViewController : UITableViewController <UINavigationControllerDelegate, UITextViewDelegate, CuisinePickerViewControllerDelegate>


@property (nonatomic, strong) IBOutlet UITextView *reviewTextView;
@property (nonatomic, strong) IBOutlet UILabel *cuisineLabel;
@property (nonatomic, strong) IBOutlet UILabel *dateLabel;
@property (strong, nonatomic) IBOutlet UILabel *addressLabel;

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, assign) double longitude;
@property (nonatomic, assign) double latitude;

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) Restaurant *restaurantToEdit;


- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;
@end

RestaurantDetailViewController.m:

- (void)viewDidLoad
{
  [super viewDidLoad];

  if (self.restaurantToEdit != nil) {
    self.navigationItem.title = @"Edit Restaurant";

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                              initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                              target:self
                                              action:@selector(done:)];
  }

  date = [NSDate date];

  self.navigationItem.title = name;
  self.reviewTextView.text = reviewText;
  self.cuisineLabel.text = @"No Cuisine selected";
  self.addressLabel.text = address;
  self.dateLabel.text = [self formatDate:date];


- (IBAction)done:(id)sender
{

  Restaurant *restaurant = nil;
  if (self.restaurantToEdit != nil) {
    restaurant = self.restaurantToEdit;
  } else {
    restaurant = [NSEntityDescription insertNewObjectForEntityForName:@"Restaurant" inManagedObjectContext:self.managedObjectContext];
  }

  restaurant.restaurantName = name;
  restaurant.restaurantReview = reviewText;
  restaurant.cuisine = cuisineType;
  restaurant.latitude = [NSNumber numberWithDouble:self.latitude];
  restaurant.longitude = [NSNumber numberWithDouble:self.longitude];
  restaurant.date = date;
  restaurant.address = address;

  NSError *error;
  if (![self.managedObjectContext save:&error]) {
    NSLog(@"Error %@", error);
    abort();
    return;
  }


  [self performSelector:@selector(closeScreen) withObject:nil afterDelay:0.6];
}

- (void)setRestaurantToEdit:(Restaurant *)newRestaurantToEdit
{
  if (restaurantToEdit != newRestaurantToEdit) {
    restaurantToEdit = newRestaurantToEdit;

    reviewText = restaurantToEdit.restaurantReview;
    cuisineType = restaurantToEdit.cuisine;
    date = restaurantToEdit.date;
    address = restaurantToEdit.address;
  }
}

mapviewcontroller.m 中的相关代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

  if ([segue.identifier isEqualToString:@"EditRestaurant"]) {

      UINavigationController *navigationController = segue.destinationViewController;
      RestaurantDetailViewController *controller = (RestaurantDetailViewController *)navigationController.topViewController;

      if (newRestaurant == YES) {
        controller.name = restaurantName;
        controller.address = restaurantAddress;
        controller.longitude = restaurantLongitude;
        controller.latitude = restaurantLatitude;
        controller.managedObjectContext = self.managedObjectContext;
      } else {
        Restaurant *restaurant = [restaurants objectAtIndex:((UIButton *)sender).tag];
        controller.restaurantToEdit = restaurant;
      }
  }
} 

我的 nslog 中唯一弹出的是 (NULL) 错误。如果您需要更多代码,请告诉我。

4

1 回答 1

3

看来,在您的 prepareForSegue 方法中,如果它是更新,则您没有设置 controller.managedObjectContext 属性。您需要添加

controller.managedObjectContext = self.managedObjectContext;

所以这就是所谓的新餐厅还是更新。

于 2013-08-20T00:06:55.607 回答