What's the scope for objective c category
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开发者_如何学JAVA affects just the files including it. Thanks in advance.
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).
精彩评论