简体   繁体   English

尝试从prepareForSegue设置ManagedObjectContext属性时出现BAD_ACCESS错误

[英]BAD_ACCESS error when trying to set ManagedObjectContext property from prepareForSegue

I am working with CoreData and I am trying to pass my ManagedObjectContext object from one ViewController to a second View Controller. 我正在使用CoreData,并且试图将我的ManagedObjectContext对象从一个ViewController传递到另一个View Controller。

Here is my code for the First View Controller: 这是我的First View Controller代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{   
    if ([[segue identifier] isEqualToString:@"showDetail"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];

        self.managedObjectContext = [self.fetchedResultsController managedObjectContext];

        [[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];
    }
}

The prepareForSegue calls a method I created called setManagedObjectContext on the second view controller: prepareForSegue在第二个视图控制器上调用我创建的名为setManagedObjectContext的方法:

-(void)setManagedObjectContext:(NSManagedObjectContext *)managedObjContext
{
    self.managedObjectContext = managedObjContext;
    //NSManagedObjectContext *context = managedObjContext;
}

When this code hits Xcode locks up and eventually throws a BAD_Access memory error. 当此代码命中时,Xcode会锁定并最终引发BAD_Access内存错误。 When I debug this code, the managedObjContext has a valid memory location but the line of code keeps hitting over and over and over again which then causes XCode to crash. 当我调试此代码时,managedObjContext具有有效的内存位置,但是代码行不断反复出现,这导致XCode崩溃。

The self.managedObjectContext is just a property I have on the SecondViewController class and its declared like this: self.managedObjectContext只是我在SecondViewController类上拥有的一个属性,其声明如下:

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

The line commented out works just fine if I use it: 如果我使用它,注释掉的行就可以了:

NSManagedObjectContext *context = managedObjContext;

So it seems like using a property is causing the problem but I certainly would like to use it. 因此,似乎使用属性会导致问题,但我当然想使用它。 Any explanation on why it would not like using the property? 关于为什么不希望使用该属性的任何解释?

Thanks! 谢谢! Flea 跳蚤

self.managedObjectContext = is equivalent to calling setManagedObjectContext: . self.managedObjectContext =等效于调用setManagedObjectContext: So you are getting yourself into a recursive loop. 因此,您将进入递归循环。

When overriding the setter of an ivar you need to access the ivar directly eg 覆盖一个ivar的setter时,您需要直接访问该ivar,例如

- (void)setManagedObjectContext:(NSManagedObjectContext *)managedObjContext;
{
  _managedObjectContext = managedObjectContext;
}

Generally you only need to override the default implementation of a setter if you are going to do additional stuff otherwise. 通常,如果您要执行其他操作,则只需要覆盖setter的默认实现即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM