harveyslash
harveyslash

Reputation: 6012

Can an Objective C method and property have the same name?

I was looking at a tutorial on core data for objective C and IOS , this is what I came across:

In the header file:

@interface CoursesTableViewController : UITableViewController <AddCourseViewControllerDelegate,NSFetchedResultsControllerDelegate>

@property (nonatomic,strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic,strong) NSFetchedResultsController *fetchedResultsController;


@end

and a method in the implementation has this:

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Course" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch

    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"author" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    _fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"author" cacheName:nil];


    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

the property fetchedResultsController and an instance method fetchedResultsController both have the exact same name.

  1. How do I distinguish between these two methods?
  2. Does this have a special meaning? (like having same name for method as class means its a constructor)

Upvotes: 2

Views: 1705

Answers (1)

Paulw11
Paulw11

Reputation: 114865

The declaration of an @property is actually creating three things for you;

  1. A backing variable to store the property,
  2. A setter method and
  3. A getter method. (Unless the property is read only in which case there is no setter method).

@property SomeType *someProperty;

Will create a backing variable (called _someProperty by default), a setter method that looks like:

-(void) setSomeProperty:(SomeType *)value {
   _someProperty=value;
}

And a getter method that looks like

-(SomeType *)someProperty {
    return _someProperty;
}

You can see that accessing a 'property' is really just method invocations.

The compiler knows that myObject.someProperty=someNewValue; is actually [myObject setSomeProperty:someNewValue] and myObject.someProperty is really [myObject someProperty]. In fact, if you single step through a line that references a property in the debugger you will see the execution jump to the property definition; this is the invocation of the getter or setter method.

In the code you have shown, the getter method has been specified explicitly so the compiler won't supply the default implementation that it normally would. In this case, this is so the property can be instantiated lazily; you see that the code checks to see if the backing variable _fetchedResultsController is not nil. If it isn't it simply returns the value. If it is then it sets up a new one, saves it to the backing variable and returns it.

When you use _propertyName you are bypassing the setter/getter. I see many programmers do this because they think it is just "shorthand" for the property, but as you can see from the example you have shown, bypassing the getter would cause a problem. You should only bypass property setters and getters when you have a reason.

One place where you should bypass the setter is in the setter itself. If you had

-(void) setSomeProperty:(SomeType *)value {
   self.someProperty=value;
}

Then you would get an infinite loop because this code is really

-(void) setSomeProperty:(SomeType *)value {
   [self setSomeProperty:value];
}

Interestingly the 'getter' syntax works for any non-void method that takes no arguments, so [NSUserDefaults standardUserDefaults] can also be written NSUserDefaults.standardUserDefaults even though standardUserDefaults isn't a property.

Upvotes: 8

Related Questions