I am new to iOS so take me slow. When i declare an object in my .h view controller named “_a” and i declare a property “a” and when i synthesize in the .m file
@synthesize a=_a;
must i use “a” or “_a” when i modify that object ? ( “a” is a UINavigationController in my case).
In another question, does my compiler automatically draw a connection from a object declared “ob” to a “_ob” declaration ?
Again, sorry for the poor explanation but this environment isn’t quite something i am use to.
An object declared like this:
And implemented like this:
Makes an ivar named
_aand two accessor methods in theExampleobject. The accessor methods have these signatures:Method
areturns the object in the_aivar. MethodsetAreleases the object stored in_a(if not nil), assigns the parameter to_a, and sends the parameter anretainmessage.These methods may also be access through dot notation:
Accessing
_adirectly will circumvent the accessor methods, potentially causing problems such as memory leaks. For example if_aholds the only reference to an object and a new object reference is assigned to_athe old object will become a leaked object.To directly answer your two questions:
You may use either
aor_a. In most cases you’ll be better off using_awhen reading the value within methods of the object declaringa, andsetA(orain dot notation) when setting the value of_a. Objects that useExampleobjects should use the accessor methods (with or without dot notation).The complier does not automatically make a connection between
oband_obdeclarations. In this example the@synthesize a = _a;statement makes the connection with the optional= _a. The ivar may have any name.@synthesize a = george;would also be valid. Without the= _apart the compiler would make an ivar namedaand two accessor methods.One further note: You may omit the declaration of
_ain the interface, which restricts the scope of the_aivar to just the implementation of the Example object. Adding the optional= _ato the@synthesizestatement will make as ivar of the same type as the property declared in the interface.