What does @synthesize window=_window do?

后端 未结 3 1429
感动是毒
感动是毒 2020-11-27 17:35

I understand that @synthesize window; combined with @property \'auto-creates\' your setters and getters, but I\'m not sure exactly what happens whe

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 18:10

    Your properties almost always have a backing variable. What

    @synthesize searchBar = _searchBar;
    

    does is declare that the backing variable for your search bar will be called _searchBar. This allows you to decouple the name of the property from the name of your variable. In fact, if you don't use @synthesize you don't need to have a backing variable at all.

    As for why people do this, everyone has different reasons. Personally, I do it to

    1. avoid clashes with variable names and
    2. make it clear when I'm using a local variable and when I'm using an instance variable.

提交回复
热议问题