Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
I'm trying to have a textfield that determines the number of decimals an app when calculating values. the problem is its not saving my the textfield value

.h
Code:
#import <Foundation/Foundation.h>

@interface Preferences : NSObject

@property (weak) IBOutlet NSTextField *DecimalsField;
-(IBAction) AddDecimal:(id)sender;
-(IBAction) SubtractDecimal:(id)sender;

@end

.m
Code:
#import "Preferences.h"

@implementation Preferences

#define kDecimalsTitle @"DecimalsTitle"
@synthesize DecimalsField = _DecimalsField;

+(void) initialize
{
    NSDictionary *defaults = [NSDictionary dictionaryWithObject:@"DecimalsTitle" forKey:kDecimalsTitle];
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}

-(void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [_DecimalsField setStringValue:[[NSUserDefaults standardUserDefaults] objectForKey:kDecimalsTitle]];
}

-(void)applicationWillTerminate:(NSNotification *)aNotification
{
    [[NSUserDefaults standardUserDefaults] setObject:[_DecimalsField stringValue] forKey:kDecimalsTitle];
}

- (IBAction)AddDecimal:(id)sender
{
    if ([_DecimalsField.stringValue isEqualToString:@"15"]) {
        return;
    } else {
        float result = [_DecimalsField intValue] + 1;
        [_DecimalsField setIntValue:result];
    }
}

- (IBAction)SubtractDecimal:(id)sender
{
    if ([_DecimalsField.stringValue isEqualToString:@"0"]) {
        return;
    }else {
        float result = [_DecimalsField intValue] - 1;
        [_DecimalsField setIntValue:result];
    }
}
@end

the code docent show any errors or warnings but it still docent save my values what have i done wrong
 
Save yourself some coding, debugging and pain by using Bindings. You can connect the text field in the .xib directly to UserDefaults this way and the loading and saving will be taken care of without any other effort on your part.
 
Several opportunies for improvement...

applicationDidFinishLaunching: seems to set the field. Don't see where the "Preferences" object is allocated in the posted code. applicationDidFinishLaunching: seems to be in the Preferences object and not the App Delegate.

Have you tried putting in some breakpoints and walking through it in the debugger? Can't diagnose without more code.

FWIW: the *number* of decimals is presumably an integer but noticed the code assigns integer values to floats. Probably not a root cause.
 
Calling synchronize not required

You have to call synchronize on NSUserDefaults after you set the value for it to store.

It is certainly possible and in some cases desirable to call synchronize but it isn't required. As Apple's docs note, synchronize is "automatically invoked at periodic intervals". See Apple's docs for the synchronize method and discussion.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.