iOS Variable names and memory management

I am a bit confused by the underscore naming and the self. Is the following code OK?

@interface MyClass()

@property (nonatomic,retain) NSString        *name;
@property (nonatomic,retain) NSString        *surname;

@end

@implementation MyClass
@synthesize name = _name;
@synthesize surname = _surname;

Then when I need to assign a value I do this

self.name = [someParam retain];

Answers


No this is not ok, since the self.name will also retain the value as your declared in the property.

This wil retain:

self.name = someParam;

Other way the write then same (be aware to release the current var):

if (_name != someParam) {
    if(_name) {
       [_name release], _name = nil;
    }
    _name = [someParam retain];
}

you don't have to repeat the retain :)

just:

self.name = someParam;

but for the rest I think it's ok


No this is not right as you have to use the alias _name like

if(_name)
{
    [_name release];
    _name=nil;
}
else
{
    //retain the object
}

Need Your Help

Selenium WebDriver with Perl

perl selenium-webdriver

I am trying to run the Selenium driver with Perl bindings, and due to the lack of examples and documentation, I am running into some roadblocks. I have figured out how to do some basic things, but ...

google map zoom out limit

javascript google-maps-api-3

How do I set zoom out limit on the map, it currently lets me zoom out too far to the point that I see multiple world maps: