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?

Était-ce utile?

La 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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top