Skip to content

Commit c4ffee6

Browse files
committed
* cleaned up JSONModelArray code
* optimized it for mutable and non-mutable arrays * fixed issues with the core data demo
1 parent c97ee9d commit c4ffee6

5 files changed

Lines changed: 47 additions & 5 deletions

File tree

JSONModel/JSONModel/JSONModel+CoreData.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ -(BOOL)updateWithDictionary:(NSDictionary*)dictionary error:(NSError**)error
9292
//copy values over
9393
for (NSString* key in [moProperties allKeys]) {
9494
id value = dictionary[key];
95-
NSLog(@"class: %@", moProperties[key]);
95+
9696
//exception classes - for core data should be NSDate by default
9797
if ([[moProperties[key]class] isEqual:[NSDate class]]) {
9898
SEL NSDateFromNSStringSelector = sel_registerName("NSDateFromNSString:");

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 : NSMutableArray
30+
@interface JSONModelArray : NSObject
3131

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

JSONModel/JSONModel/JSONModelArray.m

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

37+
-(id)firstObject
38+
{
39+
return [self objectAtIndex:0];
40+
}
41+
42+
-(id)lastObject
43+
{
44+
return [self objectAtIndex: _storage.count-1];
45+
}
46+
3747
-(id)objectAtIndex:(NSUInteger)index
3848
{
3949
id obj = _storage[index];
@@ -52,6 +62,16 @@ - (void)forwardInvocation:(NSInvocation *)anInvocation
5262
[anInvocation invokeWithTarget:_storage];
5363
}
5464

65+
-(id)forwardingTargetForSelector:(SEL)selector
66+
{
67+
static NSArray* overridenMethods = nil;
68+
if (!overridenMethods) overridenMethods = @[@"initWithArray:modelClass:",@"objectAtIndex:",@"count",@"modelWithIndexValue:",@"description",@"mutableCopy",@"firstObject",@"lastObject"];
69+
if ([overridenMethods containsObject:NSStringFromSelector(selector)]) {
70+
return self;
71+
}
72+
return _storage;
73+
}
74+
5575
- (NSUInteger)count
5676
{
5777
return _storage.count;
@@ -60,9 +80,9 @@ - (NSUInteger)count
6080
-(id)modelWithIndexValue:(id)indexValue
6181
{
6282
if (self.count==0) return nil;
63-
if (![self[0] indexPropertyName]) return nil;
83+
if (![_storage[0] indexPropertyName]) return nil;
6484

65-
for (JSONModel* model in self) {
85+
for (JSONModel* model in _storage) {
6686
if ([[model valueForKey:model.indexPropertyName] isEqual:indexValue]) {
6787
return model;
6888
}
@@ -81,7 +101,7 @@ -(id)mutableCopy
81101
-(NSString*)description
82102
{
83103
NSMutableString* res = [NSMutableString stringWithFormat:@"<JSONModelArray[%@]>\n", [_targetClass description]];
84-
for (id m in self) {
104+
for (id m in _storage) {
85105
[res appendString: [m description]];
86106
[res appendString: @",\n"];
87107
}

JSONModel/JSONModelTransformations/JSONValueTransformer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// The MIT License in plain English: http://www.touch-code-magazine.com/JSONModel/MITLicense
1616

1717
#import <Foundation/Foundation.h>
18+
#import "JSONModelArray.h"
1819

1920
/////////////////////////////////////////////////////////////////////////////////////////////
2021

@@ -72,6 +73,15 @@ extern BOOL isNull(id value);
7273
*/
7374
-(NSMutableArray*)NSMutableArrayFromNSArray:(NSArray*)array;
7475

76+
#pragma mark - NS(Mutable)Array <- JSONModelArray
77+
/**
78+
* Trasnforms an array to a JSONModelArray
79+
* @param array incoming array
80+
* @return JSONModelArray
81+
*/
82+
-(NSArray*)NSArrayFromJSONModelArray:(JSONModelArray*)array;
83+
-(NSMutableArray*)NSMutableArrayFromJSONModelArray:(JSONModelArray*)array;
84+
7585
#pragma mark - NSMutableDictionary <-> NSDictionary
7686
/**
7787
* Trasnforms a dictionary to a mutable dictionary

JSONModel/JSONModelTransformations/JSONValueTransformer.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ -(NSMutableArray*)NSMutableArrayFromNSArray:(NSArray*)array
8888
return [NSMutableArray arrayWithArray:array];
8989
}
9090

91+
#pragma mark - NS(Mutable)Array <- JSONModelArray
92+
-(NSArray*)NSArrayFromJSONModelArray:(JSONModelArray*)array
93+
{
94+
return (NSMutableArray*)array;
95+
}
96+
97+
-(NSMutableArray*)NSMutableArrayFromJSONModelArray:(JSONModelArray*)array
98+
{
99+
return (NSMutableArray*)array;
100+
}
101+
102+
91103
#pragma mark - NSMutableDictionary <-> NSDictionary
92104
-(NSMutableDictionary*)NSMutableDictionaryFromNSDictionary:(NSDictionary*)dict
93105
{

0 commit comments

Comments
 (0)