Skip to content

Commit ef03215

Browse files
committed
fixed JSONArray objectAtIndexedSubscript
1 parent 98f62f8 commit ef03215

3 files changed

Lines changed: 32 additions & 14 deletions

File tree

JSONModel/JSONModel/JSONModelArray.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
- (id)initWithArray:(NSArray *)array modelClass:(Class)cls;
3939

4040
- (id)objectAtIndex:(NSUInteger)index;
41+
- (id)objectAtIndexedSubscript:(NSUInteger)index;
4142
- (void)forwardInvocation:(NSInvocation *)anInvocation;
4243
- (NSUInteger)count;
44+
- (id)firstObject;
45+
- (id)lastObject;
4346

4447
/**
4548
* Looks up the array's contents and tries to find a JSONModel object

JSONModel/JSONModel/JSONModelArray.m

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,32 @@ - (id)initWithArray:(NSArray *)array modelClass:(Class)cls
3434
return self;
3535
}
3636

37-
-(id)firstObject
37+
- (id)firstObject
3838
{
3939
return [self objectAtIndex:0];
4040
}
4141

42-
-(id)lastObject
42+
- (id)lastObject
4343
{
44-
return [self objectAtIndex: _storage.count-1];
44+
return [self objectAtIndex:_storage.count - 1];
4545
}
4646

47-
-(id)objectAtIndex:(NSUInteger)index
47+
- (id)objectAtIndex:(NSUInteger)index
4848
{
49-
id obj = _storage[index];
50-
if (![obj isMemberOfClass:_targetClass]) {
49+
return [self objectAtIndexedSubscript:index];
50+
}
51+
52+
- (id)objectAtIndexedSubscript:(NSUInteger)index
53+
{
54+
id object = _storage[index];
55+
if (![object isMemberOfClass:_targetClass]) {
5156
NSError* err = nil;
52-
obj = [[_targetClass alloc] initWithDictionary:obj error:&err];
53-
if (obj) {
54-
_storage[index] = obj;
57+
object = [[_targetClass alloc] initWithDictionary:object error:&err];
58+
if (object) {
59+
_storage[index] = object;
5560
}
5661
}
57-
return obj;
62+
return object;
5863
}
5964

6065
- (void)forwardInvocation:(NSInvocation *)anInvocation

JSONModelDemoTests/UnitTests/ArrayTests.m

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,24 @@ -(void)testLoading
3838
XCTAssertEqualObjects([[repos.repositories[0] class] description], @"GitHubRepoModel", @".properties[0] is not a GitHubRepoModel");
3939
}
4040

41-
-(void)testCount
41+
- (void)testCount
4242
{
4343
XCTAssertEqualObjects(@(repos.repositories.count), @100, @"wrong count");
44+
}
4445

45-
NSError *err;
46-
repos = [[ReposModel alloc] initWithString:@"{}" error:&err];
46+
- (void)testReadArray
47+
{
48+
JSONModelArray *array = [JSONModelArray new];
4749

48-
XCTAssertEqualObjects(@(repos.repositories.count), @0, @"wrong count");
50+
XCTAssertEqualObjects(@(array.count), @0, @"wrong count");
51+
XCTAssertNil([array firstObject], @"first object of an empty array should be nil");
52+
XCTAssertNil([array lastObject], @"last object of an empty array should be nil");
53+
// XCTAssertThrows(array[0], @"read of empty array should throw an exception");
54+
// XCTAssertThrows(array[2], @"read of empty array should throw an exception");
55+
// XCTAssertNilThrows(array[-2], @"read of empty array should throw an exception");
56+
XCTAssertNil(array[0], @"read of empty array should be nil");
57+
XCTAssertNil(array[2], @"read of empty array should be nil");
58+
XCTAssertNil(array[-2], @"read of empty array should be nil");
4959
}
5060

5161
-(void)testFirstObject

0 commit comments

Comments
 (0)