How to make a lazy copy in lens c?

In one of the Standford IOS tutorials, the instructor uses lazy copy to instantiate the calculator engine class.

It uses the second syntax opened in my earlier question :

@synthesize myTextField = _myTextField;

      

In this syntax, the getter myTextField

has a different name from _myTextField

, so one can test

if (_myTextField != nil) { ... }

      

How do I do this with the classic first syntax, since the getter variable name and the instance name are the same ( myTextField

)?

+3


source to share


1 answer


if you are using @sythensize variableName = _variableName;

then the instance variable will be named _variableName

and that is what you need to use to access it directly. variableName

is the name that will be used to create setters and getters, soself.variableName or [self setVariableName:...]

if you use @synthesize variableName;

then the instance variable will have the same name as the synthesized setters and getters. You can still access the instance variable with variableName = ...

, but it's easier to get confused which one you should use

so 2 lazy boot runs

@synthesize varName = _varName

- (id)varName
{
    if (!_varName)
        _varName = [[NSObject alloc] init];

    return _varName;
}

      



or

@synthesize varName;

- (id)varName
{
    if (!varName)
        varName = [[NSObject alloc] init];

    return varName;
}

      

Personally I go for @synthesize varName = _varName

it is much easier to read and harder to mix up when you access a variable, when you meant the setter and vice versa

+4


source







All Articles