If I override a method in a category, will it just affect the files including it, or it affects the whole project? I want to override "methodSignatureForSelector" and "forwardInvocation" to ignore the undefined selector error for NSNull. So I want to know if this affects just the files including it. Thanks in advance.
-
Is the category in a separate file? – BoltClock May 28 '11 at 15:07
-
5Why do you want to do something so heinous as that?!?! – bbum May 28 '11 at 16:07
-
@bbum I used JSON parser which will generate NSNull object for "null" in json. I want to ignore the error when calling [title length] when title is NSNull. I am not sure if this the proper way. – Fourj May 28 '11 at 16:18
-
3No, really, you don't want to do that. Doing that will make *every single* `NSNull` throughout your app ignore such messages and that'll mask errors. If you want `null` strings to equate to empty strings, than make it so either on your server side or your parser; don't pollute your object graph with objects that behave weirdly to workaround fragility in the data model. It may seem attractively easily now, but your sanity will suffer later. – bbum May 29 '11 at 04:59
-
Thanks. Agree with you. But it's still painful to handle the NSNull value from JSONFramework. Found a practical solution: http://stackoverflow.com/questions/5716942/touchjson-dealing-with-nsnull/5719428#5719428 @bbum – Fourj May 29 '11 at 12:14
1 Answers
Say that you have a class A and a category C defined on it. Each of those classes has got its own .h .m files.
What does the category "affect"?
As to compilation, only the compilation units (.m files) that import the C.h file. Say: in files that import that header, you will not have warnings about undefined selectors (for selectors defined in C.h, of course); in the other files, you will get such warnings.
As to linking (or executing, which is pretty close in Objective-C), all of your executable is affected.
Indeed, even if you don't import the C.h file in, say, B.m, if B.m uses a selector defined in C.h, that call will succeed (i.e., the implementation from your category will be effectively used), but you will get nonetheless a warning when compiling B.m. This will also hold true if B.m was compiled at a different time (i.e, before creating the category).

- 68,819
- 11
- 102
- 123
-
Thanks @sergio. In other words there is no way that I use a category to overwrite a method and apply it to only certain .m files in my project? Thanks in advance for the clarification. – Reinhold Sep 08 '11 at 07:55
-
The last paragraph is not true, at least for Xcode 4. Xcode will issue an error instead of a warning if you don't import the category .h. Hence you can't even build successfully. Import category header is a must. – Philip007 Sep 22 '12 at 08:58