Question

Basically, I'm trying to send an object from the master view to the detail view by way of a custom setter. However, as soon as the setter gets called in the detail view, the app immediately crashes. I'm not too good at debugging, but I think it's because the object isn't making it to the setter -- it's seeing it as nil. Could be wrong about that, though.

Here's the code for the segue in the master view:

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

    CCNewsItem *theNewsItem = self.listOfNewsItems[indexPath.row];
    NSLog(@"Preparing to pass: %@", theNewsItem);
    [[segue destinationViewController] setSelectedNewsItem:theNewsItem];

    }
}

And here it is for the setter in the detail view:

   - (void)setSelectedNewsItem:(CCNewsItem *)newItem
{
    if (self.selectedNewsItem != newItem) {
    self.selectedNewsItem = newItem;

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

It crashes exactly on the - (void)setSelectedNewsItem:(CCNewsItem *)newItem line. I've made super-sure that the object being passed is valid (I log it to double check, and all seems well), but it seems that it either isn't making it to the setter or it's somehow exploding when it hits it.

Any ideas? Thanks!

As requested, here's the logs/exception

This is what my NSLog shows on the object I'm trying to pass:

2013-11-02 14:18:23.660 Curtis Consulting[14862:60b] Preparing to pass: <CCNewsItem: 0x14e70580>

When the app stops on the setter's first line (I'm not sure why; I have all breakpoints disabled), it shows the values of newItem (the passed object) as:

newItem CCNewsItem *    nil 0x00000000
NSObject    NSObject        
_headline   NSString *  nil 
_body   NSString *  nil 
_url    NSURL * nil 

Edit 2

Since, as near as I can tell, there is no way to copy the error it's showing (in a green bar at the right, but I don't have any breakpoints turned on), here's my transcription of it:

Thread 1: EXC_BAD_ACCESS (code=2, address=0x27c84ff8)
Was it helpful?

Solution

In your setter method change:

if (self.selectedNewsItem != newItem) {
self.selectedNewsItem = newItem;

to

if (_selectedNewsItem != newItem) {
_selectedNewsItem = newItem;

Because your current code has an infinite loop calling the accessor method. Be sure you know what using self. means...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top