Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Diary App Using Core Data Listing Data using NSFetchedResultsController Fetched Results Controller

Fateh Waharp
Fateh Waharp
6,039 Points

NSFetchResult

In this stage i really didn't understand what differentiate from declaring self.fetchedResultController and _fetchedResultController, because ash also did not explain why use _fetchedResultController instead of self.fetchedResultController.

here is the sample of ash's code

  • (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController != nil) { return _fetchedResultsController;

    }

here is what i thought the code would look like

  • (NSFetchedResultsController *)fetchedResultsController { if (self.fetchedResultsController != nil) { return self.fetchedResultsController;

    }

btw, ash also did not follow our coding step like what we've been teach before such as @property need to be written at .h file, he just teach like we are experienced programmer.

4 Answers

Brenden Konnagan
Brenden Konnagan
16,858 Points

No... Accessing a property through self utilizes the getter/setter methods that Xcode creates.

i.e. self.foo = 3 is telling the code in the background use the setter method for foo and pass the value 3 in.

But let's say you for some reason want to make sure that self.foo is never set to the value 3. You could instead create your own setter method that would check the value you pass in and only set (using the instance variable designated by the underbar "_foo") if the value is not 3.

Likewise you could implement special logic with getters as well.

Make sense?

Brenden Konnagan
Brenden Konnagan
16,858 Points

He is using the _fetchedResultsController because we are creating a customized getter for the property. Xcode will create setters and getters for properties by default, however we always have the option to override the default and implement our own setter/getter.

The property does not always have to be declared in the header file. It only needs to be declared in the header file when it is a public property. In this case, we do not want/need the fetchedResultsController to be accessible outside of this specific object. Thus declaring a property in the implementation file makes it private to that object only.

Does this help?

Fateh Waharp
Fateh Waharp
6,039 Points

Yeah, thanks for the reply, thats help, so, by using self. We cant change the setter and getter ?

i stumbled on the same problem but mine is even trickier ( or so I think): when customizing the getter method, if i dont use the variable with the underscore, but use a newly created one as follows:

-(NSFetchedResultsController *)fqcontrol{ if (_fqcontrol) { return _fqcontrol;} else{ NSFetchRequest *fq=[self getFq]; AppDelegate *maindelegate=[[UIApplication sharedApplication]delegate]; NSManagedObjectContext *context=maindelegate.managedObjectContext;

    NSFetchedResultsController *fqcontrol=[[NSFetchedResultsController alloc]initWithFetchRequest:fq managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];

    fqcontrol.delegate=self;
    return fqcontrol;

}

}

then ALL I GET IS A BLANK TABLE. but once I switch back to underscore way, it totally works. I know the previous one is not efficient and such, but I think all that getter method cares is to return a controlling regardless of its original name, why underscore is necessary for this to work?

Brenden Konnagan
Brenden Konnagan
16,858 Points

Hello Jack...

I had to think about this for a while... When you refer to the fqcontrol with out the under bar the compiler is looking to the object instead of a specific instance variable of the object (_fqcontrol).

Thanks Brenden, I thought about that possibility, but I DID initiate NSFetchedResultsController *fqcontrol before i return it as the result as the getter method...

there is another possibility, what i did, although returned a requestController and fulfill the purpose of getter method, the setter method is still untouched, so setter is related to _fqcontrol which might be the cause of all the problems....