Objective C interfaces, delegates, and protocols

前端 未结 3 521
难免孤独
难免孤独 2021-01-05 17:27

So I\'m trying to wrap my head around Objctive-C interfaces, delegates and protocols. So I have a question:

Does a delegate have to be in a separate file or can it

3条回答
  •  既然无缘
    2021-01-05 18:08

    • The point of a delegate is to be notified when another object does something. For example one of your objects wants to know that a window is being closed, so you register it as the window's delegate and implement the windowWillClose: method. It will be called by NSWindow appropriately. So the delegate methods are usually defined in another class. Up to a certain point, it lets you extend the functionality of a class without subclassing it.

    • (Edit: see Daniel's answer about protocols.)

    • The @interface is the class declaration, where the member variables and methods are listed. It's located in the .h, that you import if you need to use the class. The code for methods sits in the @implementation in the .m file. In Java it's different, the .java file serves both purposes.

提交回复
热议问题