-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNNSection.m
More file actions
92 lines (66 loc) · 1.83 KB
/
Copy pathNNSection.m
File metadata and controls
92 lines (66 loc) · 1.83 KB
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
//
// NNSection.m
// ArrayDiff
//
// Created by Nick Tymchenko on 21/04/14.
// Copyright (c) 2014 Nick Tymchenko. All rights reserved.
//
#import "NNSection.h"
@implementation NNSection {
@protected
id _key;
NSArray *_objects;
}
- (instancetype)init {
return [self initWithKey:nil objects:nil];
}
- (instancetype)initWithKey:(id)key objects:(NSArray *)objects {
self = [super init];
if (!self) return nil;
_key = key;
_objects = [objects copy] ?: @[];
return self;
}
- (id)objectAtIndexedSubscript:(NSUInteger)idx {
return self.objects[idx];
}
#pragma mark - NSObject
- (BOOL)isEqual:(id)other {
if (other == self) return YES;
if (!other || ![other isKindOfClass:[NNSection class]]) return NO;
return [self isEqualToSectionData:other];
}
- (BOOL)isEqualToSectionData:(NNSection *)other {
if (self.key != other.key && ![self.key isEqual:other.key]) return NO;
if (![self.objects isEqual:other.objects]) return NO;
return YES;
}
- (NSUInteger)hash {
NSUInteger prime = 31;
NSUInteger result = 1;
result = prime * result + [self.key hash];
result = prime * result + [self.objects hash];
return result;
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {
return [[NNSection allocWithZone:zone] initWithKey:self.key objects:self.objects];
}
#pragma mark - NSMutableCopying
- (id)mutableCopyWithZone:(NSZone *)zone {
return [[NNMutableSection allocWithZone:zone] initWithKey:self.key objects:self.objects];
}
@end
@implementation NNMutableSection
- (instancetype)initWithKey:(id)key objects:(NSArray *)objects {
self = [super initWithKey:key objects:objects];
if (!self) return nil;
_objects = [_objects mutableCopy];
return self;
}
- (void)setKey:(id)key {
_key = key;
}
@dynamic key;
@dynamic objects;
@end