Synthesizing a property without instance variables

前端 未结 3 540
别跟我提以往
别跟我提以往 2020-12-08 00:48

I thought I understood @property and @synthesize, but I did some experimenting and I can\'t figure out why the below (what I thought was broken) code works.

As you c

相关标签:
3条回答
  • 2020-12-08 01:19

    You can omit instance variable declaration only for 64-bit architecture

    0 讨论(0)
  • 2020-12-08 01:24

    I want to add that although the instance variable is automatically created, it's named the same as the property. By doing this, you may get bugs in your code later. So, besides preventing the case of not automatically generating on some architectures, you should always create the instance variable so that you don't get anything unexpected later on down the road.

    0 讨论(0)
  • 2020-12-08 01:39

    The quick answer is: "yes". In Objective-C 2.0, synthesized properties will automatically create the corresponding ivars as required.

    Apple's documentation has some more details.

    Important: As pointed out by Tommy (note: this is from the legacy docs - please see the latest information):

    In Objective-C 2.0 on either of the modern runtimes (ie, Intel 64bit and ARM) properties can be added to classes 'dynamically' (that is, at runtime but only before the creation of any instances — not particularly dynamic compared to the rest of the runtime). However, this can't be done on either of the two older runtimes (ie, Intel 32bit and PowerPC). It's therefore not really something you want to use on shipping software for the Mac or during development for iOS (since the simulator is a 32bit Intel application and can't create instance variables at runtime)

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