3

I apologize for duplicate Question . I am new in ios . I want to store custom objects in userdefaults. I am using objective-c .

Thanks in advance

Vinod Kumar
  • 3,375
  • 1
  • 17
  • 35
hdkumar
  • 49
  • 1
  • 7
  • 3
    Possible duplicate of [How to store custom objects in NSUserDefaults](http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults) – Rikh Apr 07 '17 at 07:20
  • 2
    Possible duplicate of [Store Custom Objects in NSUserDefaults](http://stackoverflow.com/questions/43222681/store-custom-objects-in-nsuserdefaults) – Manfred Radlwimmer Apr 07 '17 at 07:54

2 Answers2

5

First you create custom class Like below.

CustomObject.h

#import <Foundation/Foundation.h>

@interface CustomObject : NSObject<NSCoding>

@property(strong,nonatomic)NSString *name;

@end 

CustomObject.m

#import "CustomObject.h"

@implementation CustomObject



- (void)encodeWithCoder:(NSCoder *)encoder {
    //Encode properties, other class variables, etc
    [encoder encodeObject:self.name forKey:@"name"];

}

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        //decode properties, other class vars
        self.name = [decoder decodeObjectForKey:@"name"];

    }
    return self;
}

Then create CustomObject class object and store in NSUserDefaults

Stored your object like this

CustomObject *object =[CustomObject new];
object.name = @"test";

NSMutableArray *arr = [[NSMutableArray alloc]init];
[arr  addObject:object];

NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:arr];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:encodedObject forKey:@"storeObject"];
[defaults synchronize];

get custom object from NSUserDefaults like this

 NSData *storedEncodedObject = [defaults objectForKey:@"storeObject"];
        NSArray *arrStoreObject = [NSKeyedUnarchiver unarchiveObjectWithData:storedEncodedObject];
        for (int i=0; i<arrStoreObject.count; i++)
        {
            CustomObject *storedObject = [arrStoreObject objectAtIndex:i];
            NSLog(@"%@",storedObject.name);

        }
Vinod Kumar
  • 3,375
  • 1
  • 17
  • 35
  • 1
    This will not work unless your class implements protocol – Adeel Ur Rehman Apr 07 '17 at 07:29
  • Please don't add the [same answer to multiple questions](http://meta.stackexchange.com/questions/104227/is-it-acceptable-to-ad??d-a-duplicate-answer-to-several-questions). Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, tailor the post to the question. [Duplicate Answere here](https://stackoverflow.com/questions/43222681/store-custom-objects-in-nsuserdefaults/43223489#43223489) – Manfred Radlwimmer Apr 07 '17 at 07:53
0

In Swift 3

        // set a value for key
        let encodedData = NSKeyedArchiver.archivedData(withRootObject: #YOUR OBJECT#)
        UserDefaults.standard.set(encodedData, forKey: #YOUR KEY#)

        // retrieving a value for a key
        if let data = UserDefaults.standard.data(forKey:  #YOUR KEY#),
            let obj = NSKeyedUnarchiver.unarchiveObject(with: data) as? #YOUR OBJECT# {
        } else {
            print("There is an issue")
        }
Hardik Halani
  • 290
  • 2
  • 12
  • 1
    same approach and a much better explanation is here: https://stackoverflow.com/a/29987303/5881884 – DevB2F Oct 16 '17 at 17:48