Class Name with a “+”

前端 未结 2 1074
情话喂你
情话喂你 2021-01-24 19:17

I am working on an iOS project in Xcode and I see some classes that have names with a \"+\"; for example: TableViewController+TableView.h and then the class is name

相关标签:
2条回答
  • 2021-01-24 19:53

    The + in the filename isn't semantically important. Naming a file "ClassName+CategoryName.h/m" is just a popular convention for naming files containing categories.

    @interface RKTableViewController (TableView)
    @end
    

    declares a category called "TableView" on the RKTableViewController class. Categories are used to add methods to a class outside its main implementation. See the Apple documentation on categories here: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html

    0 讨论(0)
  • 2021-01-24 20:10

    These are categories. The are very helpful at times.

    You can add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are additions to a class declared elsewhere, not a new class. You cannot, however, use a category to add additional instance variables to a class.

    The methods the category adds become part of the class type. For example, methods added to the NSArray class in a category are included as methods the compiler expects an NSArray instance to have in its repertoire. Methods added to the NSArray class in a subclass, however, are not included in the NSArray type. (This matters only for statically typed objects because static typing is the only way the compiler can know an object’s class.)

    Category methods can do anything that methods defined in the class proper can do. At runtime, there’s no difference. The methods the category adds to the class are inherited by all the class’s subclasses, just like other methods.

    http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html

    Example:

    Here is an example of a category I use all the time. I don't own NSMutableArray but I would love for there to be a simple move function. Instead of subclassing just to add a simple function I attach a category.

    // NSMutableArray+Move.h
    
    @interface NSMutableArray (move)
    
    - (void)moveObjectFromIndex:(NSUInteger)from toIndex:(NSUInteger)to;
    
    @end
    
    // NSMutableArray+Move.m
    
    @implementation NSMutableArray (move)
    
    - (void)moveObjectFromIndex:(NSUInteger)from toIndex:(NSUInteger)to
    {
        if (to != from) {
            id obj = [self objectAtIndex:from];
            [self removeObjectAtIndex:from];
            if (to >= [self count]) {
                [self addObject:obj];
            } else {
                [self insertObject:obj atIndex:to];
            }
        }
    }
    

    This allows me to do new things with a class thats already been created all over my app. So anywhere I use an NSMutableArray I can call my added method like so

    NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"Object A", @"Object B", @"Object C", nil];
    
    [myArray moveObjectFromIndex:0 toIndex:2];
    
    0 讨论(0)
提交回复
热议问题