0

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
NilamPari
  • 79
  • 8
  • You can save your object using `archivedDataWithRootObject` and `unarchived` for getting the object form userdefaults.get the help from here http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults – Ravi B Sep 29 '16 at 05:13
  • Does your array contains all objects including the cart you added ? If yes do you want to fetch those array value in other controller? – Janmenjaya Sep 29 '16 at 05:21
  • ya,and i want to fetch those all values – NilamPari Sep 29 '16 at 05:32
  • @NilamPari, check out my answer and let me know if it resolves your issue and works for you?? – Janmenjaya Sep 29 '16 at 07:22

2 Answers2

1

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 ...

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
  • it gives me error like [__NSCFData count]: unrecognized selector sent to instance 0x7f964d003600 – NilamPari Sep 29 '16 at 07:29
  • I guess you are getting this error while fetching ?? And the error says like you are trying to use "count" method on NSData while it should be on array?? Are you using same method like I have done.. Add your code where you get error so that i can figure out where is the issue.. – Janmenjaya Sep 29 '16 at 07:32
  • i'm using same code which u placed,when i debug i get error at if([arrayTemp count]) { [array addObjectsFromArray:arrayTemp]; } this line of code – NilamPari Sep 29 '16 at 07:35
  • Just delete the App from simulator / device and check. Put break point and see what kind of data you are getting "arrayTemp", If it is coming as NSArray or NSData?? Let me know. And if it is coming as NSData it is for sure that you are not saving the array in UserDefault – Janmenjaya Sep 29 '16 at 07:56
  • arraytemp getting nsdata and i delete app and re run it but still gives error – NilamPari Sep 29 '16 at 08:06
  • Can you try to reset the value in NSUserDefault, in AppDelegate and then if it would work. Reset means set the value to be nil in that same key and save in userdefault, don't forget to synchronize the userDefault or else it will not update. Didi you get my point? – Janmenjaya Sep 29 '16 at 08:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124516/discussion-between-nilampari-and-janmenjaya). – NilamPari Sep 29 '16 at 09:36
0

it's very simple just get the key and save it the data in your desire data type.

Code snippet:-

save UserName:

NSUserDefaults* userName = [NSUserDefaults standardUserDefaults];
[userName rm_setCustomObject:user forKey:@"userName"];

get User Name:-

user = [userName rm_customObjectForKey:@"userName"]; 

Do like this:-

  NSArray *arrCartData=[cartObject rm_customObjectForKey:@"cart"];
 NSLog(@"Array cart Data %@",arrCartData);

if you are still facing issue please let me know.

Ravi Panchal
  • 1,285
  • 10
  • 22
  • i have done this like array = [[NSUserDefaults standardUserDefaults] rm_customObjectForKey:@"cart"]; but i want to print that value and i get null value – NilamPari Sep 29 '16 at 05:30
  • @NilamGoswami answer Edited – Ravi Panchal Sep 29 '16 at 05:36
  • if you getting the null value in array so just check while you are saving custom object it contain a data or not ?, and if you still facing this issue so i have one another option that will help you in this situation so we can discuss over that. – Ravi Panchal Sep 29 '16 at 05:38
  • still getting null value – NilamPari Sep 29 '16 at 05:45
  • 1
    save cart object like this:- `NSUserDefaults* cartData = [NSUserDefaults standardUserDefaults]; [cartData rm_setCustomObject:cart forKey:@"cart"];` – Ravi Panchal Sep 29 '16 at 06:09