TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Feb 2016 |
Maintained by Ravi.
Automatic encoding & decoding that conforms to NSCoding protocol (NSCoding / NSKeyedArchiver).
To understand how persistance can be achived, go through these useful links
Just imagine if your Model Class has hell lot of Instance Variables and you want to dump it (persistance), you will curse yourself writing code for
- (id)initWithCoder:(NSCoder *)decoder
and - (void)encodeWithCoder:(NSCoder *)encoder
To run the example project, clone the repo, and run pod install
from the Example directory first.
Ravi Prakash Sahu, [email protected]
AutoEncodeDecode is available under the MIT license. See the LICENSE file for more info.
Model Class
@interface RSClassA : NSObject <NSCoding>
@property NSString *alpha;
@property NSNumber *beta;
@property NSData *gamma;
@property NSArray *listArray;
@end
#import "RSClassA.h"
#import <AutoEncodeDecode/NSObject+NSCoding.h>
@implementation RSClassA
-(void)encodeWithCoder:(NSCoder *)coder {
[self autoEncodeWithCoder:coder];
}
-(id)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
[self autoDecode:coder];
}
return self;
}
@end
This is for extremely LAZY Ones
#import <Foundation/Foundation.h>
#import <AutoEncodeDecode/BaseModel.h>
@interface RSClassB : BaseModel
@property NSString *windows;
@property NSString *mac;
@end
#import "RSClassB.h"
@implementation RSClassB
@end
- (void)testAutoEncoderDecoder {
// Write to file
NSString *fileNameWithPath = [self getFilePath:@"Class_A_File"];
[NSKeyedArchiver archiveRootObject:[self getTestRSClassAObject] toFile:fileNameWithPath];
// Read the file
RSClassA *readObjA = [NSKeyedUnarchiver unarchiveObjectWithFile:fileNameWithPath];
NSLog(@"Alpha : %@, Gaama : %@, Array : %@", readObjA.alpha, readObjA.beta, [readObjA.listArray componentsJoinedByString:@"|"]);
// Write to file
fileNameWithPath = [self getFilePath:@"Class_B_File"];
[NSKeyedArchiver archiveRootObject:[self getTestRSClassBObject] toFile:fileNameWithPath];
// Read the file
RSClassB *readObjB = [NSKeyedUnarchiver unarchiveObjectWithFile:fileNameWithPath];
NSLog(@"Windows : %@, Mac : %@", readObjB.windows, readObjB.mac);
}
- (RSClassA*)getTestRSClassAObject {
RSClassA *classObject = [RSClassA new];
classObject.alpha = @"Test";
classObject.beta = @(10);
classObject.listArray = @[@"A", @"B"];
return classObject;
}
- (RSClassB*)getTestRSClassBObject {
RSClassB *classObject = [RSClassB new];
classObject.windows = @"I hate it";
classObject.mac = @"I love it";
return classObject;
}
- (NSString*)getFilePath:(NSString *)aFileName {
if (aFileName) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.archive", aFileName]];
}
return nil;
}