Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why change the name of getters and setters in Obj-C?

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?

like image 265
Dana Avatar asked Nov 28 '25 02:11

Dana


1 Answers

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;
like image 68
foundry Avatar answered Nov 30 '25 20:11

foundry