What is @interface UITableView (MyTableViewDelegate)

前端 未结 3 918
無奈伤痛
無奈伤痛 2021-01-29 02:05

Im looking at some source code written by someone else and its intrigues me to see this line:

@interface UITableView (MyTableViewGestureDelegate)
相关标签:
3条回答
  • 2021-01-29 02:29

    It is class category declaration - using categories you can split your class implementation into several files or add methods to existing classes.

    0 讨论(0)
  • 2021-01-29 02:41

    MyTableView : UITableView < MyTableViewGestureDelegate > says your class MyTableView - subclass of UITableView - implements the protocol named MyTableViewGestureDelegate

    UITableView (MyTableViewGestureDelegate) says you are creating a Category for the Class UITableView named MyTableViewGestureDelegate

    0 讨论(0)
  • 2021-01-29 02:43

    It is a category declaration.

    A category allows you to add methods to an existing class—even to one for which you do not have the source. Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing. Using categories, you can also distribute the implementation of your own classes among several files. Class extensions are similar, but allow additional required APIs to be declared for a class in locations other than within the primary class @interface block.

    The declaration of a category interface looks very much like a class interface declaration—except the category name is listed within parentheses after the class name and the superclass isn’t mentioned. Unless its methods don’t access any instance variables of the class, the category must import the interface file for the class it extends:

    General Syntax:

    #import "ClassName.h"
    
    @interface ClassName ( CategoryName )
    
    // method declarations
    
    @end
    

    Note that a category can’t declare additional instance variables for the class; it includes only methods. However, all instance variables within the scope of the class are also within the scope of the category. That includes all instance variables declared by the class, even ones declared @private.

    There’s no limit to the number of categories that you can add to a class, but each category name must be different, and each should declare and define a different set of methods.

    Please check the link and Example

    0 讨论(0)
提交回复
热议问题