0

I create a category to method swizzled UITextField, it's name like UITextField+MaxLength, I implement the +load method to do swizzled, but I didn't import it in prefix header or other files, just import it in SomeViewController.

I think the swizzled should only work in SomeViewController, but why the swizzled method be invoke in every UITextField even a UISearchBar? and the +load method be invoke when I run the app?

Edit

I think I need point out why I using swizzled, because of I want to using KVO to observe the text changed, so I swizzled the setText and associate a property NSNumber named displayLength, so, If I do not use KVO, I can remove the swizzled part of this category and I can do cut substring to specific max length.

Marcus Wu
  • 31
  • 7
  • That's just how swizzling works. Use the textfield delegate methods if you want to limit the length for a specific field. – dan Sep 01 '16 at 14:58
  • It is really hard to me to understand you. However, if you want to use KVO, just go ahead. You do not need to do method swizzling for it. KVO solves your problem and works on a per instance base. (This is, because it uses isa swizzling and overrides methods instead of swizzling a method.) – Amin Negm-Awad Sep 28 '16 at 02:45

1 Answers1

2

Swizzling occurs at runtime; if you perform a swizzle, it genuinely changes the implementation of that class's method. It's just as if you altered the code to UITextField.

This is why method swizzling is such a rare and dangerous thing. If you just want to alter specific instances of UITextField, subclass (or, guessing your intentions from your category title, use a delegate).

andyvn22
  • 14,696
  • 1
  • 52
  • 74
  • Hum, I think my problem is why the load be invoke when I do nothing, but In my project, I use some 3rd party just like what I do, the load did't invoke until I use it, thanks for your suggest, but If I implement the delegate method in every where I use `UITextField`, I will getting lots of duplicate code, right? – Marcus Wu Sep 01 '16 at 15:08
  • No need to duplicate code--if all of your text fields behave the same way, you can give them all the same delegate. – andyvn22 Sep 01 '16 at 15:10
  • Maybe I know what you mean, but if another developer need to implement same delegate method, I should make a subclass to the delegate? – Marcus Wu Sep 01 '16 at 15:13
  • This is sort of a different question now. Luckily, someone's already answered it: http://stackoverflow.com/questions/25223407/max-length-uitextfield – andyvn22 Sep 01 '16 at 15:24
  • I know I can do it with delegate method and I do this after I create this category, but I want to using category because of sometimes I need implement delegate method `textField:shouldChangeCharactersInRange:replacementString:` to do other things, do you know what I mean? If have another way to solve this problem, maybe I can use the other way to implement what I want. – Marcus Wu Sep 01 '16 at 15:29
  • I think I need point out why I using swizzled, because of I want to using KVO to observe the text changed, so I swizzled the `setText` and associate a property `NSNumber` named `displayLength`, so, If I do not use KVO, I can remove the swizzled part of this category and I can do cut substring to specific max length. – Marcus Wu Sep 01 '16 at 15:49