views:

68

answers:

2

Hi all,

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 [i.e. 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.

A: 

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?

deanWombourne
It is not seeming like a syntax error. I'm getting that error for each statement in Print Description and the object isn't seeming allocated rather is showing itself to be as "<variable". I don't know what "/variable" stands for. Statement: NSMutableArray *companyArray = [[NSMutableArray alloc]init]; is also showing the same error And insertion isn't working not only for companyObject, but for even a single string value written below.
neha
Hmmm, that's odd.Can you edit your question and add the code you're using for Print Description. Also add the console error. Maybe some one else will have another idea!
deanWombourne
Thanx deanWombourne for trying to help me. I don't know the exact problem, but I solved it by declaring all the variables in .h file. It solved the problem for the time-being. No variable declared locally is making itself available for access.
neha
A: 

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!