Skip to content

Commit

Permalink
* round of changes to import from NSData which NSURLSession returns o…
Browse files Browse the repository at this point in the history
…n completion
  • Loading branch information
icanzilb committed Feb 17, 2014
1 parent 48dd91b commit 5c5a717
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions JSONModel/JSONModel/JSONModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
-(instancetype)initWithDictionary:(NSDictionary*)dict error:(NSError**)err;


/**
* All JSONModel classes should implement initWithData:error:
*
* For most classes the default initWithData: inherited from JSONModel itself
* should suffice, but developers have the option ot also overwrite it if needed.
*
* @param data representing a JSON response (usually fetched from web), to be imported in the model.
* @param err an error or NULL
*/
-(instancetype)initWithData:(NSData*)data error:(NSError**)error;

/**
* All JSONModel classes should be able to export themselves as a dictionary of
* JSON compliant objects.
Expand Down Expand Up @@ -159,6 +170,8 @@ lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )

-(instancetype)initWithDictionary:(NSDictionary*)dict error:(NSError **)err;

-(instancetype)initWithData:(NSData *)data error:(NSError **)error;

/** @name Exporting model contents */

/**
Expand Down Expand Up @@ -205,6 +218,8 @@ lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )

+(NSMutableArray*)arrayOfModelsFromDictionaries:(NSArray*)array error:(NSError**)err;

+(NSMutableArray*)arrayOfModelsFromData:(NSData*)data error:(NSError**)err;

/**
* If you have an NSArray of data model objects, this method takes it in and outputs a list of the
* matching dictionaries. This method does the opposite of arrayOfObjectsFromDictionaries:
Expand Down
21 changes: 21 additions & 0 deletions JSONModel/JSONModel/JSONModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ -(id)init
return self;
}

-(instancetype)initWithData:(NSData *)data error:(NSError *__autoreleasing *)err
{
//turn nsdata to an nsstring
NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!string) return nil;

//create an instance
JSONModelError* initError = nil;
id objModel = [self initWithString:string usingEncoding:NSUTF8StringEncoding error:&initError];
if (initError && err) *err = initError;
return objModel;
}

-(id)initWithString:(NSString*)string error:(JSONModelError**)err
{
JSONModelError* initError = nil;
Expand Down Expand Up @@ -1003,6 +1016,14 @@ +(NSMutableArray*)arrayOfModelsFromDictionaries:(NSArray*)array
return [self arrayOfModelsFromDictionaries:array error:nil];
}

+(NSMutableArray*)arrayOfModelsFromData:(NSData *)data error:(NSError *__autoreleasing *)err
{
id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:err];
if (!json || ![json isKindOfClass:[NSArray class]]) return nil;

return [self arrayOfModelsFromDictionaries:json error:err];
}

// Same as above, but with error reporting
+(NSMutableArray*)arrayOfModelsFromDictionaries:(NSArray*)array error:(NSError**)err
{
Expand Down

0 comments on commit 5c5a717

Please sign in to comment.