简体   繁体   中英

Properties and ivars in MasterDetail template

After reading about properties and ivars in Objective C I'm still confused with MasterDetail template for iOS in XCode.

MasterViewController declares property for DetailViewController:

@class DetailViewController;

@interface MasterViewController : UITableViewController

@property (strong, nonatomic) DetailViewController *detailViewController;

@end

And ivar for array of objects:

@interface MasterViewController () {
    NSMutableArray *_objects;
}
@end

Why is it that way? I just can't get why those two things are declared differently. Thanks.

Declaring something as a " property " allows other objects to access and work with it. In The case above, adding " detailViewController " as a property to MasterViewController means other objects can access and work with the methods & properties DetailViewController exposes.

While the " _objects " variable is internal (or private) to the MasterViewController.

Apple's documentation is generally excellent. Apple's templates are... sometimes a little challenged. They are also sometimes slow to be updated as the language improves (or they are updated erratically). The objects array should really be a private property rather than an implementation-declared ivar. In any case, don't read too much into this.

Remember, the view controller shouldn't even be holding the data; it should be getting it from the model classes (which the template doesn't provide). Some of this is in order to keep the templates simpler to use (they're not actually example code; they're templates). Some of the weird code is due to limitations in the templating engine. (They didn't used to be able to prefix your classnames, even though they told you that you must prefix your classnames; it was very annoying.)

Unfortunately, seeing something in example code also doesn't necessarily mean it's a correct way to code. Much of Apple's example code would be completely inappropriate in production code (most of their examples lack proper model classes, or fail to handle errors correctly). But again, that's kind of the nature of example code. Focus on the coding guidelines . They're much more useful than learning from templates and examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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