简体   繁体   中英

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

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.

不,@ dynamic不会为您带来任何内存/代码大小优势

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM