iphone - Hidden features of Objective-C -
objective-c getting wider use due use apple mac os x , iphone development. of favourite "hidden" features of objective-c language?
- one feature per answer.
- give example , short description of feature, not link documentation.
- label feature using title first line.
method swizzling
basically, @ runtime can swap out 1 implementation of method another.
one clever use case lazy loading of shared resource: implement sharedfoo
method acquiring lock, creating foo
if needed, getting address, releasing lock, returning foo
. ensures foo
created once, every subsequent access wastes time lock isn't needed more.
with method swizzling, can same before, except once foo
has been created, use swizzling swap out initial implementation of sharedfoo
second 1 no checks , returns foo
know has been created!
of course, method swizzling can trouble, , there may situations above example bad idea, hey... that's why it's hidden feature.
Comments
Post a Comment