AliSoftware / OHAutoNIBi18n

Automate the Localization/Translation of your XIB & interface without any additional code nor IBOutlet!
Other
120 stars 24 forks source link

ensure that method swizzling will be called once #6

Closed adamlipka closed 9 years ago

adamlipka commented 10 years ago

When load method is called multiple times swizzling doesn't work.

AliSoftware commented 10 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?

adamlipka commented 9 years ago

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.

AliSoftware commented 9 years ago

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:

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!

adamlipka commented 9 years ago

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 :)