If I add a property to the ViewController
@property (strong, atomic) UIView *smallBox;
and synthesize it in the .m file, the variable can actually be referenced just by smallBox inside of any instance methods.
But then, self.view cannot be replaced by view, even though view is defined as a property of UIViewController too. Why the difference and what is the rule?
self.viewandview/_vieware not the same thing. Depending on how you create your instance variables,viewor_viewrefer to the actual object instance variable. It is dangerous to access this directly, and you should only do so ininit,deallocor in accessors. Everywhere else, you should useself.view.self.viewis exactly the same as[self view], which passes the message “view” to the object “self” an returns the result. By default, when an object receives a message, it executes the method with that name, and the default implementation ofviewwill return the value of the related instance variable (eitherviewor_view).In older versions of Xcode,
@synthesize viewwould create an instance variable calledview. In the latest versions of Xcode, declaring a propertyviewwill will automatically create an instance variable called_viewin many cases, even without@synthesize. This change makes it easier to notice when you are accessing the ivar directly.In short:
init,deallocand theviewaccessors (if you custom write them), always useself.view._view.@synthesizeat all. If you are writing for a slightly older Xcode, use@synthesize view=_view;self.viewdoes not mean “the value of the instance variable.” It means “the result of passing the message ‘view'” which is generally implemented as returning the instance variable.