需要修复代码“属性'managedObjectContext'中的错误,在'id'类型的对象上找不到。”

时间:2013-11-20 01:19:46

标签: ios objective-c

我为我的类编写了这段代码而且卡住了,错误是在'id'类型的对象上找不到“属性'managedObjectContext'

它落在这行代码上:

if (![self.detailItem.managedObjectContext save:&error]) 

据我所知,在代码的前面我将newDetailItem设置为id? :

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }

    if (self.masterPopoverController != nil) {
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

这是我现在要完整的代码:

#import "cavDetailViewController.h"


@interface cavDetailViewController ()
<UIAlertViewDelegate, UIActionSheetDelegate>
{
    UIAlertView *message;
}

@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end

@implementation cavDetailViewController

#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }

    if (self.masterPopoverController != nil) {
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        self.detailDescriptionLabel.text = [[self.detailItem valueForKey:@"timeStamp"] description];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configureView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)hideKeyboard:(id) sender
{
    [self.lattitude resignFirstResponder];
    [self.longitude resignFirstResponder];
    [self.description resignFirstResponder];
}

- (void) viewWillDisappear:(BOOL) pAnimated {
    [super viewWillDisappear:pAnimated];

    [self.detailItem setValue:self.lattitude.text    forKey:@"Lattitude"];
    [self.detailItem setValue:self.longitude.text    forKey:@"Longitude"];
    [self.detailItem setValue:self.description.text    forKey:@"Description"];

    NSNumber * v = [NSNumber numberWithDouble:[self.lattitude.text doubleValue]];
    NSNumber * v1 = [NSNumber numberWithDouble:[self.longitude.text doubleValue]];


    [self.detailItem setValue:v forKey:@"Lattitude"];
    [self.detailItem setValue:v1 forKey:@"Longitude"];

    // ND: do the update - ala master view code
    NSError *error = nil;
    if (![self.detailItem.managedObjectContext save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate.
        // You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }


} // end method veiwWillDisappear

#pragma mark - Split view

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = NSLocalizedString(@"Master", @"Master");
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    // Called when the view is shown again in the split view, invalidating the button and popover controller.
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    self.masterPopoverController = nil;
}

- (IBAction)bgroundColor:(id)sender
{
    [[self view] setBackgroundColor:[UIColor blueColor] ];
    [[self view] setBackgroundColor:[UIColor redColor] ];    

}



- (IBAction)dispMessage:(id)sender
{
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Have a nice day"
                                                      message:@"Have a nice day"
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];

}
@end

1 个答案:

答案 0 :(得分:1)

看来您的detailItem属性的类型为id。您应该执行以下操作之一:

  • 使用公开managedObjectContext属性或
  • 的类型声明它
  • 向展示self.detailItem属性的类添加managedObjectContext的类型转换,或
  • 更改语法以调用managedObjectContext作为方法

最后一次更改可以这样做:

NSError *error = nil;
if (![[self.detailItem managedObjectContext] save:&error])

为了使其正常工作,您的.m文件需要包含公开managedObjectContext属性的类的标头。