objective c - Do I still have to alloc my object if I'm using a property -


simple question can't seem find fast post for;

if have object, lets say:

 nsstring *mystring_;  @property(readwrite, retain)nsstring* mystring;  @synthesize mystring = mystring_;  

when synthesize this, alloc memory object?

thanks

all @synthesize generate accessors (setter , getter) property , create instance variable same name. added step of using equals sign:

@synthesize mystring = _string; 

... renames instance variable _string (or whatever name choose, underbar apple convention). practice since having instance variable share same name accessors can problematic. having underbar lets differentiate between two.

this generated (assume arc on):

// getter - (nsstring*)mystring {     return _string; } 

and

// setter - (void)setmystring:(nstring*)mystring {     _mystring = mystring; } 

you can initialize property in init method (using self.propertyname):

-(id)init {     self.mystring = @"something"; // or use alloc init method in class    } 

or can initialize lazily overriding getter so:

- (nsstring*)mystring {     if (!_mystring) {  // same saying if nil , therefore doesn't exist yet         _mystring = @"something";     }     return _mystring; } 

also note don't need declare variable separately (i.e. don't need nsstring* mystring). use strong or weak in place of retain when declaring property.

the places should access instance variable directly in accessors, allows control action through single gateway. overhead of using accessors within class (i.e self.mystring) infinitesimal. of course don't have way, it's practice.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -