/
SYPair.m
101 lines (81 loc) · 1.84 KB
/
SYPair.m
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// SYPair.m
// SYPair
//
// Created by Stan Chevallier on 10/01/2016.
// Copyright © 2016 Syan. All rights reserved.
//
#import "SYPair.h"
@interface SYPair ()
{
@protected
id _object1;
id _object2;
}
@property (nonatomic, strong) id object1;
@property (nonatomic, strong) id object2;
@end
@implementation SYPair
@synthesize object1 = _object1;
@synthesize object2 = _object2;
+ (instancetype)pairWithObject:(id)object1 andObject:(id)object2
{
return [[self alloc] initWithObject:object1 andObject:object2];
}
- (instancetype)init
{
self = [self initWithObject:nil andObject:nil];
return self;
}
- (instancetype)initWithObject:(id)object1 andObject:(id)object2
{
self = [super init];
if (self)
{
self->_object1 = object1;
self->_object2 = object2;
}
return self;
}
- (void)setObject1:(id)object1
{
[NSException raise:NSInternalInconsistencyException format:@"Object is immutable"];
}
- (void)setObject2:(id)object2
{
[NSException raise:NSInternalInconsistencyException format:@"Object is immutable"];
}
- (SYMutablePair *)mutableCopy
{
return [SYMutablePair pairWithObject:self.object1 andObject:self.object2];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p, object 1: %@, object 2: %@>",
NSStringFromClass(self.class),
self,
self.object1,
self.object2];
}
@end
@implementation SYMutablePair
@dynamic object1;
@dynamic object2;
- (instancetype)initWithObject:(id)object1 andObject:(id)object2
{
self = [super initWithObject:object1 andObject:object2];
return self;
}
- (void)setObject1:(id)object1
{
self->_object1 = object1;
}
- (void)setObject2:(id)object2
{
self->_object2 = object2;
}
- (SYPair *)copy
{
return [SYPair pairWithObject:self.object1 andObject:self.object2];
}
@end