SoFunction
Updated on 2025-04-13

IOS uses NSUserDefault to implement interface value transmission and data storage

IOS uses NSUserDefault to implement interface value transmission and data storage

The value of the system singleton NSUserDefault

In the first interface

SecondViewController *secondvc = [SecondViewController new];
//Create system singleton NSUserDefaults instance object NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//Save value[defaults setObject:_firsttextfield.text forKey:@"firstTGP"];
  NSLog(@"%@",defaults);

  [ pushViewController:secondvc animated:YES];

In the second interface

//Create an object to get stored dataNSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//Receive dataNSString *str = [defaults objectForKey:@"firstTGP"];
// Assign to the control_secondlabel.text = str;

Speaking of singletons, let’s first talk about the method of passing the value of a singleton that you rewritten by yourself. Rewrite the singleton class method by yourself and implement the value of two pages, both of which are given dead values. The reason for rewriting singletons is that you hope that it will exist in memory during the run of the program and can read data at any time.

First create a singleton class (DataHandle), two methods

#pragma mark---The first method://The reason why the singleton method uses the plus sign: There cannot be an instance object to call before creation+(instancetype)sharedDatahandle{
//For thread safety (add thread lock)  @synchronized(self) {
    if (!dataHandle) {
      dataHandle = [[DataHandle alloc]init];
    }
  }

  return dataHandle;
}

#pragma mark---The second method:
+(instancetype)sharedDatahandle{
  static DataHandle *dataHandle = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    dataHandle = [[DataHandle alloc]init];
  });

  return dataHandle;
}

Let’s give an example from behind to front. In fact, the same is true for the next

On the second page

  = [[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(backAC)];
  DataHandle *handle = [DataHandle sharedDatahandle];
   = @"Annual salary";
- (void)backAC{
  [ popViewControllerAnimated:YES];

}

Page 1

- (void)viewWillAppear:(BOOL)animated{// Value operation from back to front  //Get the value (it cannot be written in ViewDidLoad)  DataHandle *handle = [DataHandle sharedDatahandle];
   = ;
}

Hehe, it's a bit off topic. Let's continue to talk about NSUserDefault, and then talk about the data storage of NSUserDefault

At the beginning of using NSUserDefault to customize objects, you must know what types of data can be stored

1:NSNumber(NSInteger,float,double)

2:NSString,NSArray,NSDictionary,BOOL

  //Use NSUserDefault for storage  //1: Create an array to be stored  NSArray *array = @[@"11", @"22",@"33"];
  //Create NSUserDefault object  NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
  [user setObject:array forKey:@"userArray"];
  NSLog(@"%@",user);

  //Read  NSArray *newarr = [user objectForKey:@"userArray"];
  NSLog(@"%@",newarr);

#warning Note: Assigning the same Key is approximately equal to one overwrite, and the uniqueness of each Key must be ensured.// All objects stored in NSUserDefaults are immutable (this is very critical, if you make a mistake, the program will have bugs). For example, if I want to store an NSMutableArray object, I must first create an immutable array (NSArray) and then store it in NSUserDefaults.NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"123",@"234",@"456", nil];
NSArray *array1 = [NSArray arrayWithArray:mutableArray];
NSUserDefaults *user2 = [NSUserDefaults standardUserDefaults];
[user2 setObject:array1 forKey:@"What we store here must be immutable."];

// It is the same to retrieve the data. You want to use the data in NSUserDefaults to assign values ​​to the variable array./*-----------------------------------------------------------------------------------------------------------------------------
// NSMutableArray *mutablearray = [user objectForKey:@"What we store here must be immutable"];// // After writing this, mutableArray becomes an immutable array. If you want to add or delete data in the array, there will be a bug.//  NSLog(@"%@",mutablearray);

/*------  correct --------*/
NSMutableArray *mutablearray = [NSMutableArray arrayWithArray:[user2 objectForKey:@"What we store here must be immutable."]];
NSLog(@"%@",mutablearray);

Thank you for reading, I hope it can help you. Thank you for your support for this site!