objective C underscore property vs self

objective-c

Solution

In the course of your experiment, you've set up an endless loop which is why the simulator goes non-responsive.

Calling `self.detailItem` within the scope of `setDetailItem:` calls `setDetailItem:` recursively since your class implements a custom setter method for the property `detailItem`.

I would refer you to the Apple documentation on declared properties for the scoop on properties, ivars, etc; but briefly, declared properties are a simplified way of providing accessor methods for your class. Rather than having to write your own accessor methods (as we had to do before Objective-C 2.0) they are now generated for you through the property syntax.

Problem

I'm was playing around with the standard sample split view that gets created when you select a split view application in Xcode, and after adding a few fields i needed to add a few fields to display them in the detail view. and something interesting happend in the original sample, the master view sets a "detailItem" property in the detail view and the detail view displays it. ``` - (void)setDetailItem:(id) newDetailItem { if (_detailItem != newDetailItem) { _detailItem = newDetailItem; // Update the view. [self configureView]; } ``` i understand what that does and all, so while i was playing around with it. i thought it would be the same if instead of _detailItem i used self.detailItem, since it's a property of the class. however, when i used ``` self.detailItem != newDetailItem ``` i actually got stuck in a loop where this method is constantly called and i cant do anything else in the simulator. my question is, whats the actual difference between the underscore variables(ivar?) and the properties? i read some posts here it seems to be just some objective C convention, but it actually made some difference.

Original source