tags:

views:

32

answers:

1

I have declared some string properties on the app delegate for the purpose of global manipulation.

The trouble is the values that are written are not the same values that are being read. When I put debugging messages to print the values, it is familiar data. It almost looks like its being cached.

Code for declaration:

    //globals
    NSUserDefaults *options;
    NSString *strUserLogin;
    NSString *strTransKey;
    NSString *strBizName;
    NSString *strSwitchTesting;
    NSString *strDate;
    NSString *strTotalAmount;
    NSString *strTopMessage;
    NSString *strBottomMessage;
    NSString *strTransType;
    NSString *strMerchantName;
    NSString *strCustomerEmail;
    NSString *strMerchantEmail;

@property(nonatomic,retain) NSUserDefaults *options;
@property(nonatomic,retain) NSString *strUserLogin;
@property(nonatomic,retain) NSString *strTransKey;
@property(nonatomic,retain) NSString *strBizName;
@property(nonatomic,retain) NSString *strSwitchTesting;
@property(nonatomic,retain) NSString *strDate;
@property(nonatomic,retain) NSString *strTotalAmount;
@property(nonatomic,retain) NSString *strTopMessage;
@property(nonatomic,retain) NSString *strBottomMessage;
@property(nonatomic,retain) NSString *strTransType;
@property(nonatomic,retain) NSString *strMerchantName;
@property(nonatomic,retain) NSString *strCustomerEmail;
@property(nonatomic,retain) NSString *strMerchantEmail;

Load global variables with NSUserDefaults:

[NSUserDefaults resetStandardUserDefaults];
options = [NSUserDefaults standardUserDefaults];
if(options)
{
    strUserLogin = [options stringForKey:@"UserLogin"];
    strTransKey = [options stringForKey:@"TransKey"];
    strBizName = [options stringForKey:@"BizName"];     
    strSwitchTesting =[options stringForKey:@"SwitchTesting"];      
    //str date
    //total amount
    strTopMessage =[options stringForKey:@"MessageTop"];
    strBottomMessage =[options stringForKey:@"MessageBottom"];
    //trans type 
    strMerchantName = [options stringForKey:@"BizName"];
    //customer email
    strMerchantEmail =[options stringForKey:@"MerchantEmail"];
}
else {
    NSLog(@"applicationDidFinishLaunching: Error reading config file.");
}

Code to set global variables back to NSUserDefaults:

//save to disk preferences
    [options setObject: txtUserLogin.text forKey:@"UserLogin"];
    [options setObject: txtTransKey.text forKey:@"TransKey"];
    [options setObject: txtBizName.text forKey:@"BizName"];
    [options setObject: txtMerchantEmail.text forKey:@"MerchantEmail"];
    [options setObject:txtMsgTop.text forKey:@"MessageTop"];
    [options setObject:txtMsgBottom.text forKey:@"MessageBottom"];
    if(switchTesting.on)
    {
        [options setObject:@"ON" forKey:@"SwitchTesting"];
    }
    else 
    {
        [options setObject:@"OFF" forKey:@"SwitchTesting"];
    }

    //this is the magic sauce
    [options synchronize];
+1  A: 

As you're using ivars and not the properties you declared, I'm guessing your objects are getting released before you set them. However, as you're not crashing it may be something else.

Try this:

[NSUserDefaults resetStandardUserDefaults];
self.options = [NSUserDefaults standardUserDefaults];
if(options)
{
    self.strUserLogin = [options stringForKey:@"UserLogin"];
    self.strTransKey = [options stringForKey:@"TransKey"];
    self.strBizName = [options stringForKey:@"BizName"];     
    // You get the idea.
}
else {
    NSLog(@"applicationDidFinishLaunching: Error reading config file.");
}

and this

[self.options setObject: txtUserLogin.text forKey:@"UserLogin"];
[self.options setObject: txtTransKey.text forKey:@"TransKey"];
[self.options setObject: txtBizName.text forKey:@"BizName"];
[self.options setObject: txtMerchantEmail.text forKey:@"MerchantEmail"];
// ...

In general, if you declare a property, it's a good idea to use the setters and getters when updating / reading the value.

Robot K