开发者

What's are the differences between @dynamic and @synthesize?

开发者 https://www.devze.com 2022-12-16 22:22 出处:网络
From the docs I was reading that @dynamic creates the accessor methods at runtime, while @synthesize will create the accessors at build time.

From the docs I was reading that @dynamic creates the accessor methods at runtime, while @synthesize will create the accessors at build time.

So let me guess: @dynamic saves some memory and code is kept smaller in memory pages? or what? and what other differences are there between these?

Would it be ok to say: "It's always a good idea to use @dynamic rather than @synthesize"? I mean... if that saves really memory, why 开发者_开发百科not?

But I guess @dynamic has some disadvantage over @synthesize, otherwise everyone would just use @dynamic. So what's the drawbacks?


No. dynamic properties don't get generated automatically. @dynamic properties marker indicates to the compiler that you will provide the accessor and setters somehow:

From Objective-C Programming Language Guide:

@dynamic

You use the @dynamic keyword to tell the compiler that you will fulfill the API contract implied by a property either by providing method implementations directly or at runtime using other mechanisms such as dynamic loading of code or dynamic method resolution.


No, @dynamic says the implementations will be provided at runtime. @synthesize tells the compiler to create the implementations for you. You would use @dynamic with, for example, Core Data, which generates property implementations at runtime. If you don't use either, the compiler assumes you will provide the implementations.


No, @dynamic will not get you any memory/code size advantages

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号