Question

I've just learned that you can modify the automatically generated getter and setter names for Objective-C properties

// boolean property of "door" object in game
@property (strong, nonatomic, getter=isOpen) BOOL open;

I understand how isOpen is a preferable getting to just open, but why not just change the property name to isOpen?

Why would having the setter also named isOpen be not desirable?

Was it helpful?

Solution

The distinction is best appreciated if we use [] syntax

    if ([door isOpen])
       doSomething;
    else
       [door setOpen:YES];

reads more like plain English than

    if ([door isOpen])
       doSomething;
    else
       [door setIsOpen:YES];

in modern-day dot syntax the difference is a little lost

    if (door.isOpen)
       doSomething;
    else
       door.open = YES;

vs

    if (door.isOpen)
       doSomething;
    else
       door.isOpen = YES;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top