开发者

New to objective-c. I don't understand the magic in writing methods without declaration in SenTestCase

I write my first simple unit tests and don't understand the magic objective c compiler. I can write unit test methods without decl开发者_运维百科aring them in the test class .h file.

my test class remains the same empty shell:

@interface chronos2Tests : SenTestCase {
@private

}
@end

In the .m file, I can write unit test methods, which names started with test. I have thought, that I must declare all member functions in the .h file? Is it possible, to write private member functions in objective c without declaring them in the .h file?


In Objective-C, you never have to declare methods. ObjC passes messages that are dispatched at runtime. We declare methods because it provides documentation for programmers. The compiler generates warnings in many cases when you pass messages that have no matching method, but again this is to help programmers. It is legal, and in some cases even correct.


The testXXX methods are are called reflectively by SenTest and that is the reason you do not need a header file to define the methods.

Edit:

Headers main purpose in Objective-C are a hint to the compiler of how an object can be accessed while not exposing implementation details. It also serves as a form of documentation and is therefore optional. OCUnit is open source and what seems to be magic happening in your test case is really just the work of the well documented NSInvocation and Class Categories.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜