When should I use @synthesize explicitly?

后端 未结 7 1676

As far as I know, since XCode 4.4 the @synthesize will auto-generate the property accessors. But just now I have read a sample of code about NSUndoManage

7条回答
  •  不知归路
    2020-11-29 15:22

    If you do not explicitly use @synthesize the compiler will understand your property the same way if you had written

    @synthesize undoManager=_undoManager;
    

    then you will be able to write in your code things like :

    [_undoManager doSomething]; // iVar
    [self.undoManager doSomethingElse]; // Use generated getter
    

    This is the common convention.

    if you write

    @synthesize undoManager;
    

    you will have :

    [undoManager doSomething]; // iVar
    [self.undoManager doSomethingElse]; // Use generated getter
    

    Personally I stop using @synthesize, since it's not mandatory any more. For me the only reason to use @synthesize is to link an iVar to a @property. If you want to generate specific getter and setter for it. But in the given piece of code there is no iVar, I think that this @synthesize is useless. But now I think the new question is "When to use iVar ?", and I've no other response than "never" for this one !

提交回复
热议问题