OC核心内容

2025/4/20

# 分类

  • 应用场景:
    • 方法过多的时候,使用category进行分类
    • 可以给已有的第三方库/系统库类做扩展
  • 分类可以给类添加方法,但是不能添加属性
  • 操作流程:新建 -> Object-C file -> Category -> 选择基础类 -> 确定
#import "Person.h"
#import "Person+action.h"

Person* p1 = [Person new];
p1.name = @"小明";
p1.age = 20;
[p1 haha];
1
2
3
4
5
6
7

# Extension

  • Extension是OC的一个特殊的分类,只有声明没有实现
  • 特殊:延展中可以创建方法和属性
  • 使用场景:
    • 写在本类的实现中,可以给本类添加私有属性和私有方法
#import "Person.h"

// 延展的内部属性
@interface Person ()
{
  int _playYears;
}
@property NSString *lol;
- (void)sing;
@end

// 本类的方法实现
@implementation Person
- (void)sing {
    self.lol = @"亚索";
    NSLog(@"%@", self.lol);
    NSLog(@"唱歌");
}
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 协议

  • 协议protocol就是一系列方法的声明
  • 协议类似JAVA/Swift中的接口(interface),用于规定类哪些方法必须实现,哪些方法可选实现
  • 协议可以多遵守:@interface Baby : NSObject <protocolName1, protocolName2>
  • @required:(默认),必须实现的方法
  • @optional:可选实现的方法
  • 协议也可以遵守别的协议,比如:@protocol protocolName <protocolName1, protocolName2, protocolName3>

# 委托模式

  • 委托模式:配合id声明某个属性遵守的协议,实现协议中的方法
  • 协议声明
    @protocol PersonAction <NSObject>
    @required
    - (void) eat;
    
    @optional
    - (void) sleep;
    @end
    
    1
    2
    3
    4
    5
    6
    7
  • 遵守协议类
    @interface Baby : NSObject <PersonAction>
    - (void) eat;
    - (void) sleep;
    @end
    
    1
    2
    3
    4
  • 委托协议类
    @interface Person : NSObject
    @property(nonatomic, strong) NSString *name;
    // 定义一个指针,遵守PersonAction协议
    @property(nonatomic, weak) id<PersonAction> delegate;
    - (void) babySleep;
    @end
    
    // 实现
    @implementation Person
    - (void) babySleep {
        [self.delegate sleep];
    }
    @end
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
  • 泛型指针
    id<PersonAction> id1 = [Student new];
    id<PersonAction, StudentAction> id2 = [Student new];
    
    1
    2

# 单例模式

  • 规范:单例模式需要提供一个sharedInstance方法,用于获取单例对象
  • interface
    @interface Person : NSObject
    @property(nonatomic, copy)NSString* name;
    + (instancetype)sharedInstance;
    @end
    
    1
    2
    3
    4
  • implementation
    #import "Person.h"
    
    @implementation Person
    
    + (instancetype)sharedInstance {
        static Person *instance = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [[self alloc] init];
        });
        return instance;
    }
    
    + (id)allocWithZone:(struct _NSZone *)zone {
        static Person *instance = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [super allocWithZone:zone];
        });
        return instance;
    }
    
    // 如需支持拷贝(防止 copy 产生新实例):
    - (id)copyWithZone:(NSZone *)zone {
        return self;
    }
    
    - (id)mutableCopyWithZone:(NSZone *)zone {
        return self;
    }
    @end
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
  • 调用
    Person* p1 = [[Person alloc] init];
    Person* p2 = [[Person alloc] init];
    Person* p3 = [[Person alloc] init];
    
    NSLog(@"%p", p1); // 0x600000b1c070
    NSLog(@"%p", p2); // 0x600000b1c070
    NSLog(@"%p", p3); // 0x600000b1c070
    
    1
    2
    3
    4
    5
    6
    7
上次更新: 4/24/2025