-2

我在 NSDictionary 中提取和更改值有问题。

NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"dictRecensiti"];
NSLog(@"dict %@",dict);

这是输出:

dict (
    (
        {
        ingredientiRecensito = "AQUA,CETYL%20PALMITATE,PROPYLENE%20GLYCOL,SORBITOL,PRUNUS%20DULCIS,ISODODECANE,STEARIC%20ACID,CETYL%20ALCOHOL,LANOLIN,CYCLOPENTASILOXANE,CALENDULA%20OFFICINALIS,SQUALANE,HEXYLENE%20GLYCOL,FRUCTOSE,GLUCOSE,SUCROSE,UREA,DEXTRIN,ALANINE,GLUTAMIC%20ACID,ASPARTIC%20ACID,HEXYL%20NICOTINATE,TRIETHANOLAMINE,OCTYL%20METHOXYCINNAMATE,OCTYL%20COCOATE,BISABOLOL,CARBOMER,PHENOXYETHANOL,METHYLPARABEN,BUTYLPARABEN,ETHYLPARABEN,PROPYLPARABEN,SEBACIC%20ACID,CI%2047000,CI%2061565,GLYCERYL%20LINOLEATE,GLYCERYL%20LINOLENATE,BUTYLENE%20GLYCOL,ENTEROMORPHA%20COMPRESSA%20EXTRACT,ALLANTOIN,SODIUM%20METHYLPARABEN,SODIUM%20BUTYLPARABEN,SODIUM%20ETHYLPARABEN,SODIUM%20PROPYLPARABEN,HYALURONIC%20ACID,BHT,PARFUM";
        nomeRecensito = "Levissime%20Crema%20idratante%20pelli%20sensibili";
        testoRecensito = "Massimomosmaomomaodmosmodmasoosaocaoaomcoasocmosamomaodm.%20Dmosadmoasmdomasd.%20Sdoamdoamodsmao";
        tipoRecensito = "Crema%20viso%20pelli%20Sensibili";
        }
    ),
        {
        ingredientiRecensito = "WATER,GLYCERIN,PEG-7%20GLYCERYL%20COCOATE,DISODIUM%20LAURETH%20SULFOSUCCINATE,SODIUM%20COCOAMPHOACETATE,PEG-25%20HYDROGENATED%20CASTOR%20OIL,CETEARETH-60%20MYRISTYL%20GLYCOL,PEG-200%20HYDROGENATED%20GLYCERYL%20PALMATE,LAURETH-3,COCO-GLUCOSIDE,AQUAPHILUS%20DOLOMIAE%20EXTRACT,ARGININE,CAPRYLYL%20GLYCOL,CITRIC%20ACID,EVENING%20PRIMROSE%20OIL/PALM%20OIL%20AMINOPROPANEDIOL%20ESTERS,GLYCINE,OENOTHERA%20BIENNIS%20OIL,SORBIC%20ACID,TOCOPHEROL";
        nomeRecensito = "Avene%20XeraCalm%20A.D.%20olio%20detergente%20liporestitutivo";
        testoRecensito = Blblblbnnn;
        tipoRecensito = "Olio%20detergente";
        }
    )

现在我想替换 %20 ,所以我写道:

 NSString *stringV = [dict valueForKey:@"ingredientiRecensito"];
stringV = [stringV stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
NSLog(@"%@",stringV);

应用程序崩溃,这是输出:

[__NSArrayI stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0xc90f360

提前致谢

编辑:我知道这不是采用此 nsuserdefault 的最佳解决方案,因此我试图从根本上更改代码以从此 nsuserdefault 获得另一种类型的输出。非常感谢

4

2 回答 2

3

您似乎也在尝试访问数组的 valueForKey,而不是字典,因为 dict 中有多个对象。这就是为什么您会收到涉及 NSArray 的错误。

如果您希望在每个对象中替换该键的值,请遍历数组。

for (int i = 0; i < [dict count]; i++) {
    NSString *stringV = [dict[i] valueForKey:@"ingredientiRecensito"];
    stringV = [stringV stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
    NSLog(@"%@",stringV);
}

我还将 dict 的名称更改为不那么令人困惑的名称,因为这是一个包含两个字典的数组。

如果我可以补充,如果这是应用程序的一部分,我肯定会重构代码,如果这只是一个打印信息的脚本,我认为 Objective-C 可能不是执行这种操作的最佳语言。

于 2013-11-12T10:51:00.787 回答
3

查看 :

  for (NSDictionary *ingredient in dict){
         NSString *content = [ingredient objectForKey:@"ingredientiRecensito"];
         content = [content stringByReplacingOccurrencesOfString:@"%20" withString:@" "];                       
         NSLog(@"ingredient: %@", content);
  }
于 2013-11-12T10:57:39.223 回答