5

i am confused that what is difference between

NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

and

NSUserDefaults *userDefault = [[NSUserDefaults alloc] init];

can any one can help me......

thnx in advance

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98

5 Answers5

6
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

the above line gives you the singleton object by using the class method standardUserDefaults the object received by this method is allocated single memory throughout the application.

NSUserDefaults *userDefault = [[NSUserDefaults alloc] init];

this will gives you the new object, each object is allocated a different memory and deallocated when object scope is finished.

If you want to store and use the value throughout the application then its better to use singleton object and that will be deallocated when you delete your application

Suhas Arvind Patil
  • 1,732
  • 1
  • 19
  • 31
2
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

This is a class method will will return the shared instance of the NSUserDefaults. As it is a shared instance. It is the ideal place to store data at a global level, to access across your application.

NSUserDefaults *userDefault = [[NSUserDefaults alloc] init];

This is an instance method which will initialize an allocated instance of NSUserDefaults. As every-time this is called, a new instance is initialized and returned, this would not be useful to store data at a global level. Hence more suited when at an intra-class level. Also, this instance's lifetime will be valid only till its scope of reference is present.

More info here: Documentation

To access standard defaults in Simulator/Device: link

Community
  • 1
  • 1
Vijay Tholpadi
  • 2,135
  • 1
  • 15
  • 20
1

With standUserDefaults you are using the sharedInstance of the app. With alloc init you can make a new user defaults with an identifier. I used it once when I had to share data with my app's Share Extension. I saved data in User Defaults against my created Identifier to access in my Share Extension Project.

I believe there must be more explanation to it, you can look here https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/

Raheel Sadiq
  • 9,847
  • 6
  • 42
  • 54
1

[NSUserDefaults standardUserDefaults] will use global shared instance of Userdefaults.

standardUserDefaults returns a global instance of NSUserDefaults configured to search the current application's search list.

[[NSUserDefaults alloc]init] will create instance everytime.

Its better to use [NSUserDefaults standardUserDefaults]

Balaji Ramakrishnan
  • 1,909
  • 11
  • 22
  • as i read userdefault are stored in .plist file if i create with **standradUserDefault** so it is accessible at application level and what if i used alloc init , is it stored in other location (not in same plist file) – Prashant Tukadiya Apr 14 '16 at 06:46
  • No it does not get stored in the same plist. – Vijay Tholpadi Apr 14 '16 at 06:55
0

Based on NSUserDefaults.h description, when using the standardUserDefaults, you will get a global instance of NSUserDefaults configured to search the current application's search list.

While, if you use alloc init, you'll need a suitename.

-init is equivalent to -initWithSuiteName:nil

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95