RaviMalviya
12/18/2015 - 9:47 AM

encoding and decoding, save it to local database with proper mvc structure

encoding and decoding, save it to local database with proper mvc structure

@interface Note : NSObject {
    NSString *title;
    NSString *author;
    BOOL published;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *author;
@property (nonatomic) BOOL published;

- (instancetype)initWithTitle:(NSString *) titlez Author:(NSString *) authorz Published:(BOOL) publishedz;

@end



@implementation Note

@synthesize title;
@synthesize author;
@synthesize published;

- (instancetype)initWithTitle:(NSString *) titlez Author:(NSString *) authorz Published:(BOOL) publishedz
{
    self = [super init];
    if (self) {
        self.title = titlez;
        self.author = authorz;
        self.published = publishedz;
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:title forKey:@"title"];
    [encoder encodeObject:author forKey:@"author"];
    [encoder encodeBool:published forKey:@"published"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        self.title = [decoder decodeObjectForKey:@"title"];
        self.author = [decoder decodeObjectForKey:@"author"];
        self.published = [decoder decodeBoolForKey:@"published"];
    }
    return self;
}

@end


@interface SmartView : UIView

@property (nonatomic) UILabel *nameLabel;
@property (nonatomic) UIImageView *avatarImageView;

@end


@implementation SmartView

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        UILabel *nameLabel = [UILabel new];
        //configure font, color, etc
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;
    }
    return _nameLabel;
}

- (UIImageView *)avatarImageView {
    if (!_avatarImageView) {
        UIImageView * avatarImageView = [UIImageView new];
        [self addSubview:avatarImageView];
        self.avatarImageView = avatarImageView;
    }
    return _avatarImageView;
}

- (void)layoutSubviews {
    //perform layout
}

@end


@interface ViewController : UIViewController

@property (nonatomic, strong) UITableView *tableView;

@end

@interface ViewController ()

@property (nonatomic, strong) SmartView *view;

@end

@implementation ViewController
@dynamic view;

- (void)loadView {
    
    // vimal Hi vimal
    self.view = [SmartView new];
}

- (SmartView *)messageForm {
    return self.view;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.messageForm.nameLabel.text = @"Name of your";
    
    Note *note = [[Note alloc] initWithTitle:@"RAVI" Author:@"ME" Published:TRUE];
    NSArray *notes = [[NSArray alloc] initWithObjects:note, nil];
    
    //**TEST_RKHM //must read
    //comment encodeWithCoder and initWithCoder in note class you will know why we need NSCoder.
    //if create model class object and then store it local in nsuserdefault then you need
    
    // Given `notes` contains an array of Note objects
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notes];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"notes"];
    
    NSData *notesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"notes"];
    NSArray *notes1 = [NSKeyedUnarchiver unarchiveObjectWithData:notesData];
    NSLog(@"%@",notes1);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}