Skip to content

Commit 651f88f

Browse files
author
Josh Holtz
committed
Added NSCopying to model
1 parent 1f2a849 commit 651f88f

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

Classes/JSONAPIResource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#import <Foundation/Foundation.h>
1010

11-
@interface JSONAPIResource : NSObject<NSCoding>
11+
@interface JSONAPIResource : NSObject<NSCopying, NSCoding>
1212

1313
@property (nonatomic, strong) id ID;
1414
@property (nonatomic, strong) NSString *href;

Classes/JSONAPIResource.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@ - (void)linkLinks:(NSDictionary*)linked {
179179
}
180180
}
181181

182+
#pragma mark - NSCopying
183+
184+
- (id)copyWithZone:(NSZone *)zone {
185+
id copy = [[[self class] alloc] initWithDictionary:[self.__dictionary copyWithZone:zone] withLinked:nil];
186+
187+
if (copy) {
188+
// Copy NSObject subclasses
189+
[copy set__resourceLinks:[self.__resourceLinks copyWithZone:zone]];
190+
}
191+
192+
return copy;
193+
}
194+
182195
#pragma mark - NSCoding
183196

184197
- (NSArray *)propertyKeys

Project/JSONAPITests/JSONAPITests.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,24 @@ - (void)testMapKeysToProperties {
313313
XCTAssertEqualObjects(postResource.author.name, [linkedAuthor9 objectForKey:@"name"], @"Author name is not equal to %@", [post objectForKey:@"name"]);
314314
}
315315

316+
- (void)testCopying {
317+
NSDictionary *meta = @{ @"page_number" : @1, @"number_of_pages" : @5};
318+
NSDictionary *linkedAuthor9 = @{ @"id" : @9, @"name" : @"Josh" };
319+
NSDictionary *linkedAuthor11 = @{ @"id" : @11, @"name" : @"Bandit" };
320+
NSArray *linkedAuthors = @[ linkedAuthor9, linkedAuthor11 ];
321+
NSDictionary *linked = @{ @"authors" : linkedAuthors };
322+
NSDictionary *post = @{ @"id" : @1, @"name" : @"Josh is awesome", @"links" : @{ @"author" : @9 } };
323+
NSArray *posts = @[ post ];
324+
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"posts" : posts };
325+
326+
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
327+
PostResource *postResource = [jsonAPI resourceForKey:@"posts"];
328+
329+
PostResource *copyPostResource = [postResource copy];
330+
331+
XCTAssertNotEqual(postResource, copyPostResource, @"Post is not equal to copy");
332+
XCTAssertEqualObjects(postResource.name, copyPostResource.name, @"Post name is not equal to %@", copyPostResource.name);
333+
XCTAssertEqualObjects(postResource.author.name, copyPostResource.author.name, @"Author name is not equal to %@", copyPostResource.author.name);
334+
}
335+
316336
@end

0 commit comments

Comments
 (0)