Problems in loading from NSUserDefaults - objective-c

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.

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

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.

Writing in NSUserDefault and .plist failed

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

Empty value in NSUserDefaults

I am developing a cocoa application. I created a user default object with this code:
NSUserDefaults *standard_user_defaults = [NSUserDefaults standardUserDefaults];
if (standard_user_defaults) {
[standard_user_defaults setObject:myString forKey:key];
[standard_user_defaults synchronize];
}
And then, I am getting the value with this code:
NSUserDefaults *standard_user_defaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standard_user_defaults)
val = [standard_user_defaults objectForKey:key];
The problem is that if I run this application in my computer is working fine, and I can get the user default value, but if another person runs the application is getting an empty value. Any ideas?
I assume by “another person” you mean another user on your computer. The reason they cannot read what you’ve written to your user defaults is because NSUserDefaults is meant for user-specific values. To save something that your application will be able to access from both your user account and others’, you need to place it in a file stored somewhere in a shared location on the system. One way to do this would be something like this:
NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSLocalDomainMask, YES) objectAtIndex:0];
NSString *filePath = [basePath stringByAppendingPathComponent:#"someFileName"];
// write:
NSDictionary *dataToSave = [NSDictionary dictionaryWithObjectsAndKeys:theValue, #"someKey", nil];
if(filePath && [dataToSave writeToFile:filePath atomically:YES])
{
// success!
}
// read:
NSDictionary *dataToLoad = [NSDictionary dictionaryWithContentsOfFile:filePath];
if(dataToLoad)
{
// success!
NSString *theValue = [dataToLoad objectForKey:#"someKey"];
}
You should try and use valueForKey rather than objectForKey. Looking at your code, there is no reason it shouldn't work for one person and not another.
The one exception would be if you are using the defaults from the settings.bundle. Values stored into settings.bundle for user defaults are not initialized until the user opens the settings page for the first time.

problem with loading from NSUserDefaults, obj-c

I'm having a problem with NSUserDefaults. I'm saving a name, then trying to access it later in the code from another view controller, but it seems that my key is empty or not saved as when I display the string from the key my label is blank.
-(void) saveName {
NSString *name = nameField.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:name forKey:#"playersName"];
[defaults synchronize];
}
-(void) viewDidLoad // method called later on in code from different viewcontroller
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *name = [defaults objectForKey:#"playersName"];
playerName.text = name; // player name == myLabel
if ([[NSUsersDefaults standardUserDefaults] objectForKey:#"playersName"] == nil) {
playerName.text =#" blank";
}
}
My string doesn't seem to be saving to userdefaults as the #"blank" string keeps showing up with my label. I'm not too familiar with NSUserDefaults so any help is much appreciated.
A few things to note when using NSUserDefaults:
calls to syncrhonize method is not necesary and will only make it slower if your program is multi threaded.
Check if the key that you use is being used by other libraries in your project
Check if the value that you set to the key is not nil
Try to use stringForKey: or boolForKey: instead of objectForKey:
I had troubles a few times with NSUserDefaults but in the end it's usually my code that's problematic.
I would suggest you to check whether your '(void) saveName' method is being called or not... Put some breakpoint and see the result