NilamPari NilamPari - 2 months ago 15
Objective-C Question

Get value from object file using RMMapper library in iOS

I have used RMMapper library

import file

#import "NSUserDefaults+RMSaveCustomObject.h"

MyCart *cart = [[MyCart alloc]init];
cart.ProdCode = code;
cart.ProdName = s;
cart.Price = [NSString stringWithFormat:@"%f",tot];
cart.Quantity = quantity.text;
cart.Image = _imageProd;
NSMutableArray *array = [[NSUserDefaults standardUserDefaults] rm_customObjectForKey:@"cart"];
[array addObject:cart];


On other page I want to retrieve the values stored in object file.

How do I get that value?

Can anyone help me solve this problem?

Answer

As you are saving the custom model object data to user default so you need to implement the encodeWithCoder and initWithCoder method in your custom object. Similarly while saving use archivedDataWithRootObject and while fetching use unarchiveObjectWithData.

Just put this two method (encodeWithCoder and initWithCoder) in your MyCart.m

- (void)encodeWithCoder:(NSCoder *)encoder {
    //Encode properties, other class variables, etc
    [encoder encodeObject:self.ProdCode forKey:@"ProdCode"];
    [encoder encodeObject:self.ProdName forKey:@"ProdName"];
    [encoder encodeObject:self.Price forKey:@"Price"];
    [encoder encodeObject:self.Quantity forKey:@"Quantity"];
    [encoder encodeObject:self.ImageName forKey:@"ImageName"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        //decode properties, other class vars
        self.ProdCode = [decoder decodeObjectForKey:@"ProdCode"];
        self.ProdName = [decoder decodeObjectForKey:@"ProdName"];
        self.Price = [decoder decodeObjectForKey:@"Price"];
        self.Quantity = [decoder decodeObjectForKey:@"Quantity"];
        self.ImageName = [decoder decodeObjectForKey:@"ImageName"];
    }
    return self;
}

And then following code will show how to save and fetch the data from NSUserDefault

My Code To Save :

- (void)saveCart
{
    MyCart *cart = [[MyCart alloc]init];
    cart.ProdCode = @"123";
    cart.ProdName = @"Name";
    cart.Price = [NSString stringWithFormat:@"%f",100.0];
    cart.Quantity = @"20";
    cart.ImageName = @"image url";

    NSArray *arrayTemp = [[NSUserDefaults standardUserDefaults] objectForKey:@"cart"];

   // You can not use the arrayTemp to add object as the return type from `NSUserDefault` is `NSArray` which is immutable, so use another mutable array and add the object like this
    NSMutableArray *array = [NSMutableArray array];

    if([arrayTemp count])
    {
        [array addObjectsFromArray:arrayTemp];
    }

    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:cart];
    [array addObject:encodedObject];
    [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"cart"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

My Code To Fetch :

- (void)getCart
{
    NSMutableArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"cart"];

    for (NSData *encodedObject in array) {

        MyCart *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];

        NSLog(@"cart ImageName %@", object.ImageName);
    }
}

Hope it helps.

Happy coding ...