Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSUserDefaults objectForKey始终返回nil,即使密钥存在_Ios_Nsuserdefaults - Fatal编程技术网

Ios NSUserDefaults objectForKey始终返回nil,即使密钥存在

Ios NSUserDefaults objectForKey始终返回nil,即使密钥存在,ios,nsuserdefaults,Ios,Nsuserdefaults,我正在尝试以默认值保存数据。这段代码(和应用程序)第一次循环时,应该通过if语句。因为NSUserdefaults中未保存任何数据,所以还保存了名为:BoughtItem0、BoughtItem1、BoughtItem2、BoughtItem3的默认值 但是在第一次循环代码之后,第二次启动我的应用程序时,它一直在执行if语句。我的代码出了什么问题 for (int i = 0; i < [shopPrices count]; i++) { if ([[NSUserDefaults

我正在尝试以默认值保存数据。这段代码(和应用程序)第一次循环时,应该通过if语句。因为NSUserdefaults中未保存任何数据,所以还保存了名为:BoughtItem0、BoughtItem1、BoughtItem2、BoughtItem3的默认值

但是在第一次循环代码之后,第二次启动我的应用程序时,它一直在执行if语句。我的代码出了什么问题

for (int i = 0; i < [shopPrices count]; i++)
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BoughtItem%d"] == nil)
    {
        [[NSUserDefaults standardUserDefaults] setValue:@"NONO" forKey:[NSString stringWithFormat:@"BoughtItem%d", i]];
        NSLog(@"BoughtItem%d", i);
    }
    else
    {
        NSLog(@"No new bought item keys made");
    }
}

您的问题是在调用
objectForKey
时使用了格式字符串,但忽略了对
stringWithFormat
的调用。我想你的意思是-

for (int i = 0; i < [shopPrices count]; i++)
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"BoughtItem%d",i]] == nil)
    {
        [[NSUserDefaults standardUserDefaults] setValue:@"NONO" forKey:[NSString stringWithFormat:@"BoughtItem%d", i]];
        NSLog(@"BoughtItem%d", i);
    }
    else
    {
        NSLog(@"No new bought item keys made");
    }
}
for(int i=0;i<[商店价格计数];i++)
{
if([[NSUserDefaults standardUserDefaults]objectForKey:[NSString stringWithFormat:@“BoughtItem%d”,i]]==nil)
{
[[NSUserDefaults standardUserDefaults]设置值:@“NONO”forKey:[NSString stringWithFormat:@“BoughtItem%d”,i]];
NSLog(@“项目%d”,i);
}
其他的
{
NSLog(@“未制作新购买的项目密钥”);
}
}

您在setValue中有它,而不是在
if
中,因此您正在检查键“BoughtItem%d”是否存在,但您正在设置“BoughtItem0”…

尝试使用
setObject:forKey
。如果不起作用,请在设置完所有值后调用
synchronize
。谢谢!这就是我问题的答案。真不敢相信我没看到。
for (int i = 0; i < [shopPrices count]; i++)
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"BoughtItem%d",i]] == nil)
    {
        [[NSUserDefaults standardUserDefaults] setValue:@"NONO" forKey:[NSString stringWithFormat:@"BoughtItem%d", i]];
        NSLog(@"BoughtItem%d", i);
    }
    else
    {
        NSLog(@"No new bought item keys made");
    }
}