Skip to content

Commit b3126fb

Browse files
committed
Merge pull request jsonmodel#238 from smartapps-fr/master
Bring back fast enumeration to JSONModelArray
2 parents bcf6bd7 + 5800a0f commit b3126fb

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

JSONModel/JSONModel/JSONModelArray.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* of each of the objects stored in the array, it'll be converted to the target model class.
2828
* Thus saving time upon the very first model creation.
2929
*/
30-
@interface JSONModelArray : NSObject
30+
@interface JSONModelArray : NSObject <NSFastEnumeration>
3131

3232
/**
3333
* Don't make instances of JSONModelArray yourself, except you know what you are doing.

JSONModel/JSONModel/JSONModelArray.m

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ @implementation JSONModelArray
2323
Class _targetClass;
2424
}
2525

26-
- (id)initWithArray:(NSArray *)array modelClass:(Class)cls
26+
-(id)initWithArray:(NSArray *)array modelClass:(Class)cls
2727
{
2828
self = [super init];
2929

@@ -34,22 +34,22 @@ - (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
{
4444
return [self objectAtIndex:_storage.count - 1];
4545
}
4646

47-
- (id)objectAtIndex:(NSUInteger)index
47+
-(id)objectAtIndex:(NSUInteger)index
4848
{
4949
return [self objectAtIndexedSubscript:index];
5050
}
5151

52-
- (id)objectAtIndexedSubscript:(NSUInteger)index
52+
-(id)objectAtIndexedSubscript:(NSUInteger)index
5353
{
5454
id object = _storage[index];
5555
if (![object isMemberOfClass:_targetClass]) {
@@ -62,22 +62,22 @@ - (id)objectAtIndexedSubscript:(NSUInteger)index
6262
return object;
6363
}
6464

65-
- (void)forwardInvocation:(NSInvocation *)anInvocation
65+
-(void)forwardInvocation:(NSInvocation *)anInvocation
6666
{
6767
[anInvocation invokeWithTarget:_storage];
6868
}
6969

7070
-(id)forwardingTargetForSelector:(SEL)selector
7171
{
7272
static NSArray* overridenMethods = nil;
73-
if (!overridenMethods) overridenMethods = @[@"initWithArray:modelClass:",@"objectAtIndex:",@"objectAtIndexedSubscript:", @"count",@"modelWithIndexValue:",@"description",@"mutableCopy",@"firstObject",@"lastObject"];
73+
if (!overridenMethods) overridenMethods = @[@"initWithArray:modelClass:",@"objectAtIndex:",@"objectAtIndexedSubscript:", @"count",@"modelWithIndexValue:",@"description",@"mutableCopy",@"firstObject",@"lastObject",@"countByEnumeratingWithState:objects:count:"];
7474
if ([overridenMethods containsObject:NSStringFromSelector(selector)]) {
7575
return self;
7676
}
7777
return _storage;
7878
}
7979

80-
- (NSUInteger)count
80+
-(NSUInteger)count
8181
{
8282
return _storage.count;
8383
}
@@ -114,4 +114,32 @@ -(NSString*)description
114114
return res;
115115
}
116116

117+
-(NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
118+
objects:(id __unsafe_unretained [])stackbuf
119+
count:(NSUInteger)stackbufLength
120+
{
121+
NSUInteger count = 0;
122+
123+
unsigned long countOfItemsAlreadyEnumerated = state->state;
124+
125+
if (countOfItemsAlreadyEnumerated == 0) {
126+
state->mutationsPtr = &state->extra[0];
127+
}
128+
129+
if (countOfItemsAlreadyEnumerated < [self count]) {
130+
state->itemsPtr = stackbuf;
131+
while ((countOfItemsAlreadyEnumerated < [self count]) && (count < stackbufLength)) {
132+
stackbuf[count] = [self objectAtIndex:countOfItemsAlreadyEnumerated];
133+
countOfItemsAlreadyEnumerated++;
134+
count++;
135+
}
136+
} else {
137+
count = 0;
138+
}
139+
140+
state->state = countOfItemsAlreadyEnumerated;
141+
142+
return count;
143+
}
144+
117145
@end

JSONModelDemoTests/UnitTests/ArrayTests.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,19 @@ -(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");
4444
}
4545

46-
- (void)testReadArray
46+
-(void)testFastEnumeration
47+
{
48+
for (GitHubRepoModel *m in repos.repositories) {
49+
XCTAssertNoThrow([m created], @"should not throw exception");
50+
}
51+
}
52+
53+
-(void)testReadArray
4754
{
4855
JSONModelArray *array = [JSONModelArray new];
4956

0 commit comments

Comments
 (0)