Writing in NSUserDefault and .plist failed - objective-c

I'm having trouble getting some informations written in my .plist, and NSUser Defaults Files.
For the .plist i've already posted but, nothing seems to be wrong:
How to update an array set into the .plist dictionary
And for the NSUser Default, i'm doing something like this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *userSettings = [[NSMutableArray alloc]initWithArray:[[defaults objectForKey:#"userSettings"]mutableCopy]];
[userSettings addObject:userSettingsArray];//adding an array into my userSettingsArray (declared higher in the code)
[defaults setValue:userSettings forKey:#"userSettings"];
[defaults synchronize];
I was wandering, is there any option in Xcode allowing , or not, the user to write into the app folders?
I'm a little stuck here
Thank you for your help,
Tim

Use
[defaults setObject:userSettings forKey:#"userSettings"];
instead of
[defaults setValue:userSettings forKey:#"userSettings"];

I found the solution to my problem:
I entered the datas from the IBOutlet like this
[self.tempUserSettings insertObject:self.firstName atIndex:1];
instead of this:
[self.tempUserSettings insertObject:self.firstName.text atIndex:1];
so the datas were wrong, and so and so.....
Thank you for you help, learned a lot from those .plist and NSUserDefault

Related

How to change the size of the system cursor in a macosx cocoa app programmatically using swift or objective-c?

I've tried to set it's global size using this code:
-(void)setOption {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *olddict = [defaults persistentDomainForName:#"com.apple.universalaccess"];
NSMutableDictionary *newdict = [olddict mutableCopy];
[newdict setObject:#4.0 forKey:#"mouseDriverCursorSize"];
[defaults setPersistentDomain:newdict forName:#"com.apple.universalaccess"];
[defaults synchronize];
NSLog(#"Cursor size set to %#", newdict);
}
And I can see in the NSLog that it changed it but I don't know how to restart/reset the system cursor in order for the cursor to change to the specified size.
Does anybody know a better way to change it's size programmatically or how to restart the system cursor after the defaults change?
EDIT(about duplication): My question is unique because I can't use applescript in resolving this like the answer provided in the other topic. Also the topic has been created in 2013, and seems outdated. Maybe things have changed a little since then. Also maybe Swift would be a viable solution for resolving this. Who knows? All these arguments make it clear that this is not a duplicate question.
CGError state = CGSShowCursor(CGSDefaultConnection) ;
if (state != kCGErrorSuccess) NSLOG(#"error : %d",state);
maybe try it with CGSShowCursor(CGSMainConnectionID())
This might also help : https://github.com/alexzielenski/Mousecape/blob/1d534b1e076b07a01b80364be23c88c8439028bc/Mousecape/mousecloak/NSCursor_Private.h
Warning. This code is not based on what is saved in preferences so combine it:
float cursorScale = 2;
cursorScale = MAX(1, MIN(cursorScale,4));
int connectionID = CGSMainConnectionID();
CGSSetCursorScale(connectionID, cursorScale);
to get the size
CGSGetCursorScale(connectionID, &cursorScale);
,
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *olddict = [defaults persistentDomainForName:#"com.apple.universalaccess"];
NSMutableDictionary *newdict = [olddict mutableCopy];
[newdict setObject:#4.0 forKey:#"mouseDriverCursorSize"];
[defaults setPersistentDomain:newdict forName:#"com.apple.universalaccess"];
[defaults synchronize];
NSLog(#"Cursor size set to %#", newdict);
CREDITS: Alex Zielenski

NSUserDefaults error for "objectAtIndex"

Getting an error message on this line of code:
Team *team = [leagueLocal.teams objectAtIndex:indexpath.row];
Error is "Use of undeclared identifier 'indexpath'"... but I'm not sure what I would put at objectAtIndex: other than indexpath.row, because thats what works for me in the TableView part of this app.
Big picture, I'm trying to use NSUserDefaults save the Team Name that was selected. Team Names are being pulled in via JSON from a web service.
Here is the full code for the NSUserDefaults part I have so far:
// Create User Defaults Variable
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Tell it where to look
Sport *sportLocal = [sports objectAtIndex:0];
League *leagueLocal = [sportLocal.leagues objectAtIndex:0];
Team *team = [leagueLocal.teams objectAtIndex:indexpath.row]; // This is where error is
// Store the data, this is the stuff I want to save
[defaults setObject:team.abbreviation forKey:[NSString stringWithFormat:#"teamAbbreviation%d", _buttonNumber]];
// After saving, synchronize data
[defaults synchronize];
Thanks for the help! Let me know if any more info is needed, or where I can help clarify if needed.
Edit- I added an NSLog in my viewDidLoad right below the error to see what I was getting, and it came back null: NSLog(#"object at index: %#", team.abbreviation);
If this is inside viewDidLoad, then unless you've initialized the indexPath variable it will not be valid. It's only valid in your tableView methods because it's passed in through the function.
You have a couple options to grab the indexPath that your looking for though:
Team *team = [leagueLocal.teams objectAtIndex:[NSIndexPath indexPathForRow:i inSection:k];
//where i and k are integers that indicate the row/section for the tableViewCell your looking for...(k would just be equal to 0 if you only have one section)
Or another way would be if the user has previously selected what your looking for then you can use:
NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow];
Team *team = [leagueLocal.teams objectAtIndex:indexPath.row];//now you can use it the way you have it written in your question
Hope it helps!

Saving NSUserDefaults Issue?

i have been working on a app, and needed to store a string. I used this code to set the default:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:CertificateKey.stringValue forKey:#"SavedKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#",[defaults objectForKey:#"SavedKey"]);
I loged it, so i know it saved...well, it showed me the value.
When i open my application, I use this to retrieve the default:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[CertificateKey setStringValue:[defaults objectForKey:#"SavedKey"]];
[CertificateKey setTitleWithMnemonic:[defaults objectForKey:#"SavedKey"]];
[[NSUserDefaults standardUserDefaults] synchronize];
Why will it not get the default value? Did i not completely save it?
Don't quit the application by pressing the stop button in xcode. Instead, quit it by right clicking on the application icon and selecting "Quit".
Edit
Maybe the first time that you execute the application, you want to save some defaults but you don't want to set them the second+ time that the application runs.
For this purpose in some class initialize method register the defaults, like this:
+ (void) initialize
{
NSUserDefaults* defaults= [NSUserDefaults standardUserDefaults];
[defaults registerDefaults: #{} ];
// This case it's an empty dictionary, but you can put whatever you want inside it.
// just convert it to data if it's not an object storable to a plist file.
}
Also, you're using setValue:forKey: , that method is inherited from NSObject. Use setObject:forKey: .
And use finalize if you want to save the defaults at the end:
- (void) finalize
{
// Save the defaults here
}
You might have a problem because you are creating an instance of NSUserDefaults. From what I understand you are supposed to access it like so: [[NSUserDefaults standardUserDefaults] setValue:#"Pikachu" forKey:#"Best Pokemon Ever"]; [[NSUserDefaults standardDefaults] objectForKey:#"Best Pokemon Ever"]; Rather than actually creating an instance of it.

Problems in loading from NSUserDefaults

I have a very strange problem while my app loads from NSUserDefaults.
This is the code in my appDelegate:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *user = [[NSString alloc]initWithString:[defaults objectForKey:#"_settingsUsername"]];
NSString *password = [[NSString alloc]initWithString:[defaults objectForKey:#"_settingsPassword"]];
NSString *instance = [[NSString alloc]initWithString:[defaults objectForKey:#"_settingsInstance"]];
The error is this: [NSPlaceholderString initWithString:]: nil argument'
It's very strange because i put in Settings.bundle -> Root.plist the default values for all the fields above.
Oddly that's how it's supposed to work -- this confused me too initially.
I guess the thing to remember is that you can use NSUserDefaults without the Settings.bundle, so it can't be the only way to set default values.
Apple provide sample code, AppPrefs, that shows how to copy the default value from the settings to NSUserDefaults.
Why don't you just use that?
NSUserDefaults *defaults = [NSUserDefaults standarduserDefaults];
NSString *user = [defaults objectForKey:#"_settingsUsername"];
NSString *password = [defaults objectForKey:#"_settingsPassword"];
NSString *instance = [defaults objectForKey:#"_settingsInstance"];
And make sure that there are objects for these keys.
hope it helps
I think the problem is that if the user hasn't gone into the settings for your app yet then all values returned from NSUserDefaults for those keys are nil. You need to handle that case and act accordingly - probably by going into the Settings.bundle and picking out the default value that you have put in there. I would just write a method that gets the value for a key and if NSUserDefaults returns nil it handles all the querying of the settings bundle for you.

How to fetch the value saved in NSUserdefault

I am trying to use NSUser Deafault in my application user have tp fillup 4 UITextFields. I am trying to save the fields so that on subsequent runs of the program that whatever they previously put will already be displayed in those UITextFields so the wont have to re-input it in - unless they want to edit something in which case they still have that option. I think that I have figured out a good way to save the strings using NSUserDefaults but now I am trying to figure out how to have those fields populate a UITextField - it doesnt seem as easy as if they were UILabels. This is the route I am attempting:
Now my value is being saved in nsuserDefalut,but hot how to fetch dat value next time(when i re start application (null appear on the uitext field)) ..I am trying this cods
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults]; //Hooks.
NSString *placeHolderName = [userData stringForKey:#"name"];
urltextFieldNormal.text = placeHolderName;
Do you remember to sync NSUserDefaults when you save the strings?
[[NSUserDefaults standardUserDefaults] setObject:#"Hello mister nick" forKey:#"alarmsArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
Cheers mate...