简体   繁体   中英

UserDefaults not saving values iphone

This' is a strange problem I'm facing. The following code which I used to save my values to nsuserdefaults was working fine.. saving and retrieving all the values but it's totally driving me nuts now. It's not saving any values and the retrieved values are seen invalid. I'm using encoding and decoding to save my custom object named companyObject. Not a single values is stored.

-(void)saveCustomObject
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSMutableArray *companyArray = [[NSMutableArray alloc]init];
for(Company *companyObj in companyObjectsArray)
{
    [companyArray addObject: [NSKeyedArchiver archivedDataWithRootObject:companyObj]];
}

[defaults setObject:companyArray forKey:@"companyData"];
[defaults setObject:userCompanyInfo.sessionkey forKey:@"sessionkey"];
[defaults setObject:userCompanyInfo.deviceid forKey:@"deviceid"];
[defaults setObject:userCompanyInfo.userid forKey:@"userid"];
[defaults setObject:userCompanyInfo.service_type forKey:@"service_type"];
[defaults setObject:userCompanyInfo.isduallogin forKey:@"isduallogin"]; 
[defaults setObject:userCompanyInfo.securitycode forKey:@"securitycode"];   

[defaults synchronize]; 
}



-(void)loadCustomObject
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSMutableArray *companies = [NSMutableArray array];
NSArray *oldCompanies = [[NSUserDefaults standardUserDefaults] arrayForKey:@"companyData"];
if( companies )
    {
        for( NSData *data in oldCompanies )
            {
                Company* companyObj = (Company*) [NSKeyedUnarchiver unarchiveObjectWithData:data];
                [companies addObject:companyObj];
            }
    }

NSString *sessionKey = [defaults stringForKey:@"sessionkey"];
NSString *deviceId = [defaults stringForKey:@"deviceid"];
NSString *userId = [defaults stringForKey:@"userid"];
NSString *service_type = [defaults stringForKey:@"service_type"];
NSString *isduallogin = [defaults stringForKey:@"isduallogin"];
NSString *securitycode = [defaults stringForKey:@"securitycode"];
}

Can anybody please help?

Between I'm running this on simulator. All the values [ie strings here] are in place, only not getting inserted. Also I'm getting this line printed in console if I do Print Description of variables in which I'm storing the values: A syntax error in expression, near `variable)'.

EDIT: This application when this part was running, was actually running on MAC pc but now I'm running it on macbook. Can this create this problem?

Thanx in advance.

Because you have had this method working in the past you can compare it with any changes you've made since then. Do you have some code in your version control from when it was working?

I'd guess that will show that you've changed Company somehow and it's not encoding properly?


PS What, exactly, are you typing to try to 'Print Description' - if it's a syntax error we should be able to fix it pretty easily?

If you want to use the simulator and get variable values, you need to build&debug with the xcode debug configuration (not release or distribution). Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM