Is it possible to view the exact code generated by @synthesize?
-
Soitanly. I though I could learn something from viewing that code. – cfischer Oct 13 '10 at 22:14
-
Related: http://stackoverflow.com/a/589348/412916 http://stackoverflow.com/questions/917884/ http://cocoawithlove.com/2009/10/memory-and-thread-safe-custom-property.html – Jano Dec 20 '11 at 23:30
2 Answers
Sure it is. Go to the .m file where you've @synthesized
the property, then from the Build menu, choose "Show Assembly Code". The compiler conveniently puts in stuff like:
.align 4, 0x90
"-[Foo setStr:]":
Leh_func_begin2:
Lfunc_begin2:
....
And
.align 4, 0x90
"-[Foo answer]":
Leh_func_begin3:
Lfunc_begin3:
....
Oh, you wanted Objective-C code? Sorry, this is done by the compiler, and the compiler reads Objective-C, but writes in Assembly.

- 242,470
- 58
- 448
- 498
You can read the Clang compiler source to see the C++ code that generates the getter and setter methods:
http://llvm.org/svn/llvm-project/cfe/trunk/lib/CodeGen/CGObjC.cpp
It's pretty cryptic but that's the most accurate way of seeing the code generated.
You can also look at the objc_getProperty and objc_setProperty implementations in Apple's runtime implementation. While not literally the same as the code that is generated, this code is much easier to read and gives a clear indication of what a proper getter and setter should do.

- 14,858
- 2
- 41
- 43
-
If "clear" includes having lots of dead code in your file, then yeah. But you're right, the good stuff is buried in there as well. – Steven Kramer May 03 '11 at 13:14