Need explanation on @property/@synthesize in iOS

前端 未结 2 1017
悲哀的现实
悲哀的现实 2021-01-31 16:24

Not a complete noob I am quite new to iOS programming and to Ojbective-C. I mainly come from a background of C (DSP, Microcontrollers), Delphi XE2/Pascal , Visual Basic and Java

相关标签:
2条回答
  • 2021-01-31 17:07

    This is a language feature that has had its usage evolve rapidly over the past few years, which explains the various forms. With the most recent tools, you can choose to ignore @synthesize and have things work reasonably.

    The default behavior in that case produces the same effect as @synthesize list = _list;.

    0 讨论(0)
  • 2021-01-31 17:16

    There is no correct way. There is only your preferred style.

    The lastest compilers do an implicit synthesise on the property declaration.

    @synthesize list = _list; .

    Nothing is ever written in your code. It just happens.

    However that doesnt stop you doing this explicitly.

    @synthesize list = somethingelse;

    So when you ask for a pointer to list via the accessor (self.list) you will get a pointer to somethingelse

    In most cases NSMutableArray *thing = self.list is equivalent to NSMutableArray *thing = somethingelse

    And just because Apple uses a style doesn't mean that you have to do it. Each company usually has their own coding style.

    The main problem with using @synthesize list; is that it poses the risk that you can write either

    self.list = thing or list = thing .

    The former uses the sythesised setList: accessor while the latter doesn't and put the risk of related bugs in your code , though its not as bad with ARC as you dont get leaks happening for strong properties.

    What ever style you use, keep it consistent and be aware of the effects of using an ivar directly list = thing as compared to using its accessor self.list = thing

    0 讨论(0)
提交回复
热议问题