0

I have a class called "EntityType" that has a string "name" and I am using NSUserDefaults to save it so I tried this:

if (button.selected)
{
    [button setSelected:YES];
    [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithBool:YES] forKey:@"buttonSelected"];
}

I want to access the variable from another class and save it inside NSUserDefaults

PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
aboodmanna
  • 31
  • 1
  • 9
  • Why would you save the selected state of a button in `NSUserDefaults`? – Droppy Jul 25 '16 at 08:33
  • because I have a condition depending on the pressed button – aboodmanna Jul 25 '16 at 08:35
  • So you are using `NSUserDefaults` to pass information between different parts of your app? – Droppy Jul 25 '16 at 08:36
  • yes I am doing this – aboodmanna Jul 25 '16 at 08:37
  • 1
    Then you are mis-using the system. Have a downvote. – Droppy Jul 25 '16 at 08:38
  • Because you are unable to manage data effectively within your app and are resorting to hacky solutions. What's more you cannot even get your hacks to work. – Droppy Jul 25 '16 at 08:39
  • I am not getting your question, can you please elaborate it? – Saurabh Jain Jul 25 '16 at 08:43
  • Droppy! I have to use NSUserDefaults because the saved value will never be changed – aboodmanna Jul 25 '16 at 08:55
  • What he means is, you are going about this problem all wrong. NSUserDefaults is used for persisting data between each execution of your app. As for passing data around inside your app there are numerous options. A common technique is to use delegation (have a look at the accepted answer in this post: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – oyvindhauge Jul 25 '16 at 08:57
  • First of all you can not store Class value inside NSUserDefaults you need to create Class Object and then Pass Your values through segue or push. Or you need to archive into data and then store in user default – Viraj Padsala Jul 25 '16 at 10:28

4 Answers4

2

You have missed sync call.Apply this to save it.

if (button.selected)
{
    [button setSelected:YES];
    [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithBool:YES] forKey:@"buttonSelected"];
     [[NSUserDefaults standardUserDefaults] synchronize];

}

to get it back later

NSNumber* savedValue = [[NSUserDefaults standardUserDefaults]
    objectForKey:@"buttonSelected"];

This is if you want to store only the value. If you want to store your custom object look at this link How to store custom objects in NSUserDefaults

Community
  • 1
  • 1
l.vasilev
  • 926
  • 2
  • 10
  • 23
  • where should I put the string "name" which is a property in my EntityType class ? – aboodmanna Jul 25 '16 at 08:21
  • You don't need the synchronize this will just force the writing of User Defaults to File, which happens periodically anyway. You can read and write to Defaults in memory, without writing to file. – Ryan Heitner Jul 25 '16 at 08:27
  • I still don't get it, where should I put the value (not the key) of the string "name" which is a property in my EntityType class ? – aboodmanna Jul 25 '16 at 08:38
0

You can get NSNumber object like,

 NSNumber *num =  [[NSUserDefaults standardUserDefaults] objectForKey:@"buttonSelected"];

from anywhere in the app.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

On your EntityType class, implement the following two methods for encoding and decoding (something relevant to your own object):

- (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;
}

Reading and writing from NSUserDefaults:

- (void)saveCustomObject:(MyObject *)object key:(NSString *)key {
    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:encodedObject forKey:key];
    [defaults synchronize];

}

- (MyObject *)loadCustomObjectWithKey:(NSString *)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *encodedObject = [defaults objectForKey:key];
    MyObject *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
    return object;
}

Code borrowed from: save class in NSUserDefaults

Viraj Padsala
  • 1,388
  • 1
  • 13
  • 31
0
**ViewController.h**

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
//here i took one userdefault property
@property(strong,nonatomic)NSUserDefaults *user;
@end



**ViewController.m**

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad 
{
    [super viewDidLoad];
    //after the property declaration take standard user defaults
    _user = [NSUserDefaults standardUserDefaults];

    //here i took Bool and key you can take any thing but took my_val
    [_user setBool:YES forKey:@"my_val"];

}


 // this is button action
 - (IBAction)submit:(id)sender

 {
      //for bool i am checking the condition given above
       if ([_user boolForKey:@"my_val"])
    {

        **enter code here**

        NSLog(@"values is set to yes");
    }

    else

    {

         **enter code here**

         NSLog(@"Value is set to No");
    }

}