Closed adamlipka closed 9 years ago
Hi,
Thanks for your Pull Request
I wonder when this would actually be the case. Normally, +initialize
methods could be called multiple times (by the class and when using one of its subclass too), but +load
will only be called once, only when the library is loaded by the runtime. So I wonder which case could trigger such behavior of having +load
being called multiple times?
Could you explain in which case you needed this?
Sorry for huge delay in response, I'll try to describe when +load OHAutoNIBi18n is called twice.
I had to use +load method in some UIView subclass. Like this:
@implementation FooClass (UIView subclass)
+(void) load { [super load]; ...... }
this call [super load] causes that calling +load OHAutoNIBi18n twice and swizzle doesn't work. I had to remove [super load] or make this fix in order to make localisation working.
Ok thanks for your feedback.
I see what was happening here. You should NEVER call [super load]
in your load
. That may be disturbing but that's because +load
is a very special method that is totally unlike other methods.
That is, +load
does not follow the normal rules of methods like inheritance and all. In fact, that very special +load
method is called by the runtime when a class is loaded in memory, and the runtime only calls +load
on a class or category if it is directly implemented in that class or category. So unlike any other methods:
+load
in class Parent
and don't override it in the subclass Child
of class Parent
, then the +load
implementation of Parent
won't be called+load
on a category even if it is also implemented in the class (unlike any other method on categories, as you normally are not supposed to override in your category a method of the class it extends). That's because +load
is very special and is not really a standard method that follows inheritance and category rules.So that +load
method is very special and also you really have to understand its uncanny and specific behaviors to use it properly (for example when +load
gets called, there may not be any @autoreleasepool
so you also have to set it up yourself, etc).
Bottom line, you should never call [super load]
and doing so may lead to unexpected behavior, as you just described. Note that all of this is not related to OHAutoNIBi18n
at all, that's just how +load
works in any case, so those rules applies for the rest of your code and any other library!
Thank you for such quick response.
Of course, at the end, I removed [super load]
call.
This was caused by AppCode. In general when I want to implement/override some method i choose ctrl+o
and select method from the list. AppCode do the rest, prepare empty method with super
call at the first line :)
When load method is called multiple times swizzling doesn't work.