简体   繁体   English

UserDefaults不保存值iPhone

[英]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. 我用来将值保存到nsuserdefaults的以下代码运行正常。.保存并检索所有值,但现在完全让我发疯了。 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. 我正在使用编码和解码来保存名为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)'. 另外,如果我执行以下操作,则会在控制台中打印此行:打印要在其中存储值的变量的描述:表达式中的语法错误,在`variable)'附近。

EDIT: This application when this part was running, was actually running on MAC pc but now I'm running it on macbook. 编辑:此部分正在运行时,此应用程序实际上在MAC pc上运行,但是现在我在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? 我猜这表明您以某种方式更改了Company,并且编码不正确?


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? PS:您到底要键入什么来尝试“打印说明” -如果是语法错误,我们应该可以很容易地修复它?

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). 如果要使用模拟器并获取变量值,则需要使用xcode调试配置(而非发行版或发行版)进行构建和调试。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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