When do I define objective-c methods?

后端 未结 3 938
悲哀的现实
悲哀的现实 2020-12-23 00:18

I\'m learning Objective-C, and have a C/C++ background.

  • In object-oriented C++, you always need to declare your method before you define (implement) it, e

3条回答
  •  抹茶落季
    2020-12-23 00:29

    Objective-C treats functions as "messages" and as such, you can send a "message" to any object - even one that doesn't explicitly state in its interface that it can accept. As a result, there are no such things as private members in Obj-C.

    This can be very powerful, but is a source of confusion for new Obj-C programmers - especially those coming from C++, Java or C#. Here are the basic rules of thumb:

    • You should define all public methods in your @interface so that consumers know what messages you expect to handle.
    • You should define @private methods in your @interface to avoid compiler messages and avoid having to order the methods in your @implementation.
    • You should use protocols when implementing a particular convention of methods for your class.

    Much of this is personal preference, however it helps to avoid annoying compiler warnings and keeps your code organized. and easy to understand.

提交回复
热议问题