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
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
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
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
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/
[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]
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