Is the name of localized "jp" or "jap"
in
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:#"jp or jap"] forKey:#"AppleLanguages"];
You can sort your array as following
NSArray *sortedArray = [yourArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
localizedCaseInsensitiveCompare is method in NSString.
try with this,
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:#"ja_JP"] forKey:#"AppleLanguages"];
Related
I have fetched the image from image picker and uploaded the image to web service and i have a image url.I have saved the imageurl using user defaults as given below:
-(void)postSignupService {
[NewJsonHelperClass postExecuteWithParams:#"signup" secondParm:[self signUpParams] onCompletion:^(NSDictionary *json){
if ([[json valueForKey:#"status"] isEqualToString:#"success"]) {
NSDictionary *dataDict =[NSDictionary new];
dataDict =[json valueForKey:#"user"];
[[NSUserDefaults standardUserDefaults] setObject:#"sucess" forKey:#"LoginStatus"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"_id"] forKey:#"user_Id"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"userName"] forKey:#"userName"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"password"] forKey:#"Password"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"email"] forKey:#"email_Id"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"profile_pic"] forKey:#"image_Str"];
NSLog(#"datadict is %#",dataDict);
SelectFoodVC *selectVc = [appDelegateRef.storyBoard instantiateViewControllerWithIdentifier:#"SelectFoodVC"];
[self.navigationController pushViewController:selectVc animated:YES];
// TabBarController *tabVc = [appDelegateRef.storyBoard instantiateViewControllerWithIdentifier:#"TabBarController"];
// [self.navigationController pushViewController:tabVc animated:YES];
}
else{
[reUse showAlertWithTitle:[json valueForKey:#"status"] message:[json valueForKey:#"user"] thirdParam:self];
}
}];
}
-(NSDictionary *)signUpParams {
NSMutableDictionary *params =[NSMutableDictionary new];
params[#"userName"] =self.userNameTf.text;
params[#"email"] =self.emailTf.text;
params[#"password"] =self.passwordTf.text;
NSLog(#"imgstr is %#",imgStr);
params[#"profile_pic"] =imgStr;
return params;
}
Here profile pic value is imgStr which is got as imageurl from web service.
When i try to retrieve the imageurl in different viewcontroller,the value of userdefault is null as it shows error.
NSString *urlstring = [[NSUserDefaults standardUserDefaults] objectForKey:#"image_Str"];
NSLog(#"urlstring is %#",urlstring);
Here urlstring is null and the error is Error: Request failed: not found (404)
NSString *str = [NSString stringWithFormat:#"http://swiftdeveloperblog.com/wp-content/uploads/2015/07/1.jpeg"];
NSURL *url = [NSURL URLWithString:str];
[[NSUserDefaults standardUserDefaults]setURL:url forKey:#"URL"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSString *getURL = [[NSUserDefaults standardUserDefaults]URLForKey:#"URL"];
NSLog(#"url is %#",getURL);
I have an NSMutableArray. The array is storing NSMutableDictionary. The dictionary object is storing NSUserDefault values. These are slider values previously selected by user. Now I want that, when user move these sliders then new slider values must be saved in NSUserDefaults.
I'm using this method to add a value in the NSMutableArray
NSMutableArray *quarterValueArray = [[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"] mutableCopy];
if (!quarterValueArray)
{
NSMutableArray *aQuarterArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++)
{
NSMutableDictionary *dict1 = [[[NSMutableDictionary alloc] init] mutableCopy];
[dict1 setObject:#"" forKey:#"quarterP"];
[dict1 setObject:#"" forKey:#"quartersliderValue"];
[dict1 setObject:#"" forKey:#"totalOfTheQuater"];
[aQuarterArray addObject:dict1];
}
[[NSUserDefaults standardUserDefaults] setObject:aQuarterArray forKey:#"QuarterValues"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
and then for storing slider values i am using this method in sliderviewCntroller
-(void)saveUserDefaultDataQuarter
{
NSMutableDictionary *quarter = [QuarterValueArray objectAtIndex:0];
[quarter setObject:totalQuarter1txt.text forKey:#"totalOfTheQuater"];
[quarter setObject:showQuartertxt.text forKey:#"quarterP"];
[quarter setObject:[NSString stringWithFormat:#"%d",(int)slider4.value]forKey:#"quartersliderValue"];
}
QuarterValueArray =[[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"]mutableCopy];
Why this code is crashing when the method is called second time? The crash log says:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
Try this.
NSMutableArray *quarterValueArray = [[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"] mutableCopy];
if (!quarterValueArray)
{
NSMutableArray *aQuarterArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++)
{
NSMutableDictionary *dict1 = [[[NSMutableDictionary alloc] init] mutableCopy];
[dict1 setObject:#"" forKey:#"quarterP"];
[dict1 setObject:#"" forKey:#"quartersliderValue"];
[dict1 setObject:#"" forKey:#"totalOfTheQuater"];
[aQuarterArray addObject:dict1];
}
[[NSUserDefaults standardUserDefaults] setValue:aQuarterArray forKey:#"QuarterValues"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[self saveUserDefaultDataQuarter];
NSLog(#"%#",quarterValueArray);
and modify like this.
-(void)saveUserDefaultDataQuarter{
NSMutableArray*QuarterValueArray =[[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"]mutableCopy];
NSMutableDictionary *quarter = [QuarterValueArray objectAtIndex:0];
[quarter setValue:totalQuarter1txt.text forKey:#"totalOfTheQuater"];
[quarter setValue:showQuartertxt.text forKey:#"quarterP"];
[quarter setValue:[NSString stringWithFormat:#"%d",(int)slider4.value]forKey:#"quartersliderValue"]; }
UPDATE :I have used set value for setting the dictionary key values, as dictionary is already created when we are modifying values.
When setting NSUserDefaults, I was initially using this code to set the defaults...
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"string1", #"string2", #"string3", nil];
[[NSUserDefaults standardUserDefaults] setObject:array forKey: #"preset1"];
[[NSUserDefaults standardUserDefaults] synchronize];
I've learned that I should be using this instead:
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"string1", #"string2", #"string3", nil];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:array forKey:#"preset1"];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
Now I'm having an issue manipulating the objects later on in array. Here is the code I use to add/remove strings from array. It worked fine when I was initially setting the defaults manually in my first example. Now, the objects will not remove from the array. I did notice when printing the array in LLDB debugger that array is now being stored as a NSCFArray when it was just an NSArray before.
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"preset1"]];
NSArray *stringsToRemove = #[#"string1", #"string2" ];
for (NSUInteger i = 0; i < stringsToRemove.count; i++) {
[array removeObjectIdenticalTo:[stringsToRemove objectAtIndex:i]];
}
[[NSUserDefaults standardUserDefaults] setObject:array forKey: #"preset1"];
[[NSUserDefaults standardUserDefaults] synchronize];
This code works for me with your setup, after initializing the defaults the second way you described:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *array = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:#"preset1"]];
NSArray *stringsToRemove = [NSArray arrayWithObjects:#"string1", #"string2", nil];
for (NSString *aString in stringsToRemove) {
[array removeObjectIdenticalTo:aString];
}
[defaults setObject:array forKey: #"preset1"];
[defaults synchronize];
I'm new to Obj-C, and I have a little problem here.
I'm trying insert the values from a temp Dictionary into an array
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myDict setObject:[NSNumber numberWithInt:100] forKey:#"Record"];
[myDict setObject:#"www" forKey:#"Name"];
[myArray addObject:myDict];
[myDict setObject:[NSNumber numberWithInt:80] forKey:#"Record"];
[myDict setObject:#"eee" forKey:#"Name"];
[myArray addObject:myDict];
[myDict setObject:[NSNumber numberWithInt:70] forKey:#"Record"];
[myDict setObject:#"rrr" forKey:#"Name"];
[myArray addObject:myDict];
[myDict setObject:[NSNumber numberWithInt:0] forKey:#"Record"];
[myDict setObject:#"ttt" forKey:#"Name"];
[myArray addObject:myDict];
NSLog(#"myArray %#",myArray);
Regardless of the bad implementation code here (and my bad English as well :p), what I'm trying to figure out is how do I clean myDict after each insertion in myArray.
Using [myDict removeAllObjects]; after each addObject is giving me an empty array at the end.
Thanks!
Thanks for all your answers, with [myArray addObject:[[myDict copy] autorelease]]; the code works just fine.
The problem here is you're adding the exact same mutable dictionary to the array over and over. So your array is simply a collection of references to the exact same object. Therefore it shouldn't be surprising that every object in your array is identical to the last one.
In order to do what you want, you have two options. The first is to copy your mutable dictionary each time and add that copy to the array. You can do this by using
[myArray addObject:[[myDict copy] autorelease]];
The other solution is to use separate dictionaries to begin with. Instead of mutating a mutable dictionary each time, you could construct a brand new NSDictionary:
[myArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:100], #"Record",
#"www", #"Name",
nil]];
[myArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:80], #"Record",
#"eee", #"Name",
nil]];
I think you need to copy the dictionary before you add it to the array...
[myArray addObject:[[myDict copy] autorelease]];
Then you can use [myDict removeAllObjects] as normal.
NSMutableDictionary * as * suggests is a pointer to the object of dictionary.
So when you add it to an array it is still the same object - pointed by myDict as well as by the element in the array.
Therefore, I suggest you create new dictionary after each add:
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myDict setObject:[NSNumber numberWithInt:100] forKey:#"Record"];
[myDict setObject:#"www" forKey:#"Name"];
[myArray addObject:myDict];
myDict = [[NSMutableDictionary alloc] init];
[myDict setObject:[NSNumber numberWithInt:80] forKey:#"Record"];
[myDict setObject:#"eee" forKey:#"Name"];
[myArray addObject:myDict];
I assume you are using ARC in the code above.
Although, the more Objective-C'ish way would be to do:
[myArray addObject: [NSDictionary dictionaryForObjectsAndKeys: [NSNumber numberWithInt:80], #"Record", #"www", #"Name"]];
if i'm doing this
NSArray * myArray = [[NSArray alloc] init];
myArray = [[NSUserdefaults standardUserDefaults] objectForKey:#"array"];
[myArray release];
am i destroying value stored in NSUserDefaults for key #"array"? Is it still extractable or not already? Does [[NSUserdefaults standardUserDefaults] objectForKey:#"array"]; return pointer or value?
Your call to objectForKey is returning an autoreleased array.
Don't do the alloc/init or release thing around that. You'd be leaking and most likely crashing.
Just do:
NSArray * myArray = [[NSUserDefaults standardUserDefaults] objectForKey: #"array"];
am i destroying value stored in NSUserDefaults for key #"array"?
NO
Is it still extractable or not already?
Extractable
Does [[NSUserdefaults standardUserDefaults] objectForKey:#"array"]; return pointer or value?
Pointer. Returns pointer to object.
Now your code is incorrect. You can rewrite it like this:
NSArray * myArray = [[[NSUserdefaults standardUserDefaults] objectForKey:#"array"] retain];
....
[myArray release];
It sounds to me like you want to get rid of the value. What you are doing is making an array with the data from [[[NSUserdefaults standardUserDefaults] objectForKey:#"array"].
If you want to get the data and use it elsewhere:
NSArray * myArray = [[[NSUserDefaults standardUserDefaults] objectForKey:#"array"] retain];
// do stuff here
[myArray release];
just like beryllium said. Of course, you could have to set it first:
[[NSUserDefaults standardUserDefaults] setObject:anObject forKey:#"array"];
If you want to get rid of the data:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"array"];
Because I honestly do not know what you are skiing, it is very hard to give you the answer you are looking for. Could you please clear it up a little?
Store array
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:yourArray forKey:#"yourKey"];
[userDefaults synchronize];
Retrieve
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *yourArr = [userDefaults objectForKey:#"yourKey"];