JSONModelArray.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // JSONModelArray.m
  3. //
  4. // @version 1.0.2
  5. // @author Marin Todorov, http://www.touch-code-magazine.com
  6. //
  7. // Copyright (c) 2012-2014 Marin Todorov, Underplot ltd.
  8. // This code is distributed under the terms and conditions of the MIT license.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  13. //
  14. // The MIT License in plain English: http://www.touch-code-magazine.com/JSONModel/MITLicense
  15. #import "JSONModelArray.h"
  16. #import "JSONModel.h"
  17. @implementation JSONModelArray
  18. {
  19. NSMutableArray* _storage;
  20. Class _targetClass;
  21. }
  22. -(id)initWithArray:(NSArray *)array modelClass:(Class)cls
  23. {
  24. self = [super init];
  25. if (self) {
  26. _storage = [NSMutableArray arrayWithArray:array];
  27. _targetClass = cls;
  28. }
  29. return self;
  30. }
  31. -(id)firstObject
  32. {
  33. return [self objectAtIndex:0];
  34. }
  35. -(id)lastObject
  36. {
  37. return [self objectAtIndex:_storage.count - 1];
  38. }
  39. -(id)objectAtIndex:(NSUInteger)index
  40. {
  41. return [self objectAtIndexedSubscript:index];
  42. }
  43. -(id)objectAtIndexedSubscript:(NSUInteger)index
  44. {
  45. id object = _storage[index];
  46. if (![object isMemberOfClass:_targetClass]) {
  47. NSError* err = nil;
  48. object = [[_targetClass alloc] initWithDictionary:object error:&err];
  49. if (object) {
  50. _storage[index] = object;
  51. }
  52. }
  53. return object;
  54. }
  55. -(void)forwardInvocation:(NSInvocation *)anInvocation
  56. {
  57. [anInvocation invokeWithTarget:_storage];
  58. }
  59. -(id)forwardingTargetForSelector:(SEL)selector
  60. {
  61. static NSArray* overridenMethods = nil;
  62. if (!overridenMethods) overridenMethods = @[@"initWithArray:modelClass:",@"objectAtIndex:",@"objectAtIndexedSubscript:", @"count",@"modelWithIndexValue:",@"description",@"mutableCopy",@"firstObject",@"lastObject",@"countByEnumeratingWithState:objects:count:"];
  63. if ([overridenMethods containsObject:NSStringFromSelector(selector)]) {
  64. return self;
  65. }
  66. return _storage;
  67. }
  68. -(NSUInteger)count
  69. {
  70. return _storage.count;
  71. }
  72. -(id)modelWithIndexValue:(id)indexValue
  73. {
  74. if (self.count==0) return nil;
  75. if (![_storage[0] indexPropertyName]) return nil;
  76. for (JSONModel* model in _storage) {
  77. if ([[model valueForKey:model.indexPropertyName] isEqual:indexValue]) {
  78. return model;
  79. }
  80. }
  81. return nil;
  82. }
  83. -(id)mutableCopy
  84. {
  85. //it's already mutable
  86. return self;
  87. }
  88. #pragma mark - description
  89. -(NSString*)description
  90. {
  91. NSMutableString* res = [NSMutableString stringWithFormat:@"<JSONModelArray[%@]>\n", [_targetClass description]];
  92. for (id m in _storage) {
  93. [res appendString: [m description]];
  94. [res appendString: @",\n"];
  95. }
  96. [res appendFormat:@"\n</JSONModelArray>"];
  97. return res;
  98. }
  99. -(NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
  100. objects:(id __unsafe_unretained [])stackbuf
  101. count:(NSUInteger)stackbufLength
  102. {
  103. NSUInteger count = 0;
  104. unsigned long countOfItemsAlreadyEnumerated = state->state;
  105. if (countOfItemsAlreadyEnumerated == 0) {
  106. state->mutationsPtr = &state->extra[0];
  107. }
  108. if (countOfItemsAlreadyEnumerated < [self count]) {
  109. state->itemsPtr = stackbuf;
  110. while ((countOfItemsAlreadyEnumerated < [self count]) && (count < stackbufLength)) {
  111. stackbuf[count] = [self objectAtIndex:countOfItemsAlreadyEnumerated];
  112. countOfItemsAlreadyEnumerated++;
  113. count++;
  114. }
  115. } else {
  116. count = 0;
  117. }
  118. state->state = countOfItemsAlreadyEnumerated;
  119. return count;
  120. }
  121. @end