The following is working in objective c:
// Base Class in ClassA.h and ClassA.m
@interface ClassA : NSObject
- (NSString *) myMethod;
@end
@implementation ClassA
- (NSString*) myMethod { return @"A"; }
@end
//Category in ClassA+CategoryB.h and ClassA+CategoryB.m
@interface ClassA (CategoryB)
- (NSString *) myMethod;
@end
@implementation ClassA (CategoryB)
- (NSString*) myMethod { return @"B"; }
@end
The question is, if I am just importing ClassA.h and send the message
[myClassA myMethod]; //returns B
why is this returning B
? I am not importing ClassA+CategoryB
Even futhrer, if I did the following:
// Base Class in ClassA.h and ClassA.m
@interface ClassA : NSObject
- (NSString *) myMethod;
- (NSString *) mySecondMethod;
@end
@implementation ClassA
- (NSString*) myMethod { return @"A"; }
- (NSString *) mySecondMethod { return [self myMethod]; }
@end
//Category in ClassA+CategoryB.h and ClassA+CategoryB.m
@interface ClassA (CategoryB)
- (NSString *) myMethod;
@end
@implementation ClassA (CategoryB)
- (NSString*) myMethod { return @"B"; }
@end
and call mySecondMethod:
ClassA *a = [[ClassA alloc] init];
NSLog(@"%@",[a myMethod]);
the result will still be B
although nobody knows (due to no import) of the category implementation?!
I'd excepted, only to return B
if I was importing the category...
So any hints appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…