面试题答案
一键面试类别(Category)
- 语法定义:
- 类别用于在不子类化的情况下,为已有的类添加方法。其语法格式为:
@interface ClassName (CategoryName) // 方法声明 - (void)categoryMethod; @end @implementation ClassName (CategoryName) - (void)categoryMethod { // 方法实现 NSLog(@"This is a category method."); } @end
- 类别只能添加方法,不能添加实例变量。如果类别中声明的方法与原类中的方法同名,会覆盖原类的方法。
- 示例:假设已有一个
NSString
类,我们为其添加一个类别来获取字符串的字符数量(不使用length
属性的方式)。
使用时:@interface NSString (CustomCharacterCount) - (NSUInteger)customCharacterCount; @end @implementation NSString (CustomCharacterCount) - (NSUInteger)customCharacterCount { NSUInteger count = 0; unichar charBuffer[100]; [self getCharacters:charBuffer range:NSMakeRange(0, self.length)]; for (NSUInteger i = 0; i < self.length; i++) { if (charBuffer[i] != 0) { count++; } } return count; } @end
NSString *str = @"Hello"; NSLog(@"Custom character count: %lu", (unsigned long)[str customCharacterCount]);
扩展(Extension)
- 语法定义:
- 扩展是一种特殊的类别,它没有名字(匿名类别),用于在类的实现文件(
.m
文件)中为类声明私有方法和添加实例变量(实际上是通过关联对象来模拟添加实例变量)。其语法格式为:
@interface ClassName () // 私有方法声明 - (void)privateMethod; // 这里不能直接声明实例变量,可通过关联对象模拟 @end @implementation ClassName - (void)privateMethod { // 方法实现 NSLog(@"This is a private method."); } @end
- 扩展是一种特殊的类别,它没有名字(匿名类别),用于在类的实现文件(
- 示例:假设我们有一个
Person
类,在其实现文件中使用扩展添加一个私有方法。
使用时:// Person.h @interface Person : NSObject - (void)publicMethod; @end // Person.m @interface Person () - (void)privateMethod; @end @implementation Person - (void)publicMethod { NSLog(@"This is a public method."); [self privateMethod]; } - (void)privateMethod { NSLog(@"This is a private method."); } @end
Person *person = [[Person alloc] init]; [person publicMethod]; // 不能直接调用 [person privateMethod]; 因为是私有方法
主要不同点总结:
- 类别有名字,扩展通常没有名字(匿名)。
- 类别主要用于为现有类添加公开方法,扩展主要用于在类的实现文件中添加私有方法和模拟实例变量。
- 类别可在任何地方定义,扩展一般在类的实现文件中定义。