【问题标题】:NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x10256a1b0'NSInvalidArgumentException',原因:'-[__NSCFConstantString objectForKey:]:无法识别的选择器发送到实例 0x10256a1b0'
【发布时间】:2017-12-13 09:16:59
【问题描述】:

我有一个视图控制器,我在其中解析 xml 文件并在字典中获取其值,我试图获取标签中的值,但我的应用程序因显示此错误而崩溃*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x10256a1b0'

我从字典中获取值的代码是这些,

 NSLog(@"string: %@", str);
NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:str];
NSLog(@"dictionary: %@", xmlDoc);

for (NSDictionary * oneCustomer in xmlDoc)
{
    // Create a new Customer record

    _lblID.text = [oneCustomer objectForKey:@"_id"];
    NSLog(@"Reg: %@ ",_lblID.text);

    _lblAlert.text = [oneCustomer objectForKey:@"_topic"];
    NSLog(@"Make: %@ ",_lblAlert.text);

    _lblxref.text = [oneCustomer objectForKey:@"xref"];

    NSLog(@"Type: %@ ",_lblxref.text);

    _lbltext.text = [oneCustomer objectForKey:@"text"];
    NSLog(@"Model: %@ ", _lbltext.text);

    _lblgraphic.text = [oneCustomer objectForKey:@"explanation"];
    NSLog(@"Model: %@ ",_lblgraphic.text);

    _lblprompt.text = [oneCustomer objectForKey:@"prompt"];
    NSLog(@"Model: %@ ",_lblprompt.text);

   _lblvoice.text = [oneCustomer objectForKey:@"_id"];
    NSLog(@"Model: %@ ", _lblvoice.text);

    //roils = [oneCustomer objectForKey:@"recommended oil"];
    //NSLog(@"Model: %@ ",roils);
}

如何消除此错误? 这是我的字典,

dictionary: {
"__name" = QF;
"_category" = "C&M";
"_id" = AB2001;
"_ni_exempt" = no;
"_topic" = Alertness;
answers =     {
    answer =         (
                    {
            "_correct" = no;
            text = "give an arm signal as well as using your indicators";
        },
                    {
            "_correct" = no;
            text = "signal so that other drivers can slow down for you";
        },
                    {
            "_correct" = yes;
            text = "look over your shoulder for a final check";
        },
                    {
            "_correct" = no;
            text = "select a higher gear than normal";
        }
    );
};
question =     {
    explanation =         {
        text = "If you want to make a U-turn, slow down and ensure that the road is clear in both directions. Make sure that the road is wide enough to carry out the manoeuvre safely.";
        voice =             {
            "_id" = "AB2001-2";
        };
    };
    prompt = "Mark one answer";
    text = "Before you make a U-turn in the road, you should";
    voice =         {
        "_id" = "AB2001-1";
    };
    xref = "DES s4, DES s9, HC r159-161";
};

我必须从这本字典中提取所有值我怎样才能得到这个?

【问题讨论】:

  • 显然在某些时候oneCustomer 是一个NSString 对象,而不是NSDictionary,所以你不能对它执行[oneCustomer objectForKey:someKey] 并导致崩溃。
  • 那么现在该如何解决呢?如何从字典中获取值? @Larme
  • 这不是字典。你能给我们str的值吗?我有一些猜测,比如为什么 for 循环......所以 _lblID.text = [xmlDoc objectForKey:@"_id"]; 而不是 _lblID.text = [oneCustomer objectForKey:@"_id"];(等等),但这是疯狂的猜测......
  • 我已经添加了字典,请检查它。@Larme
  • 删除for (NSDictionary * oneCustomer in xmlDoc),然后只做_lblID.text = [xmlDoc objectForKey:@"_id"];(等等,将oneCustomer替换为xmlDoc

标签: ios objective-c


【解决方案1】:

希望对你有帮助,

-(void)fetchValuesFromDictioanry:(NSDictionary*)xmlDict
{
    for (NSString *string in [xmlDict allKeys])
    {
        if ([[xmlDict valueForKey:string] isKindOfClass:[NSDictionary class]])
        {
            [self fetchValuesFromDictioanry:[xmlDict valueForKey:string]];
        }
        else if ([[xmlDict valueForKey:string] isKindOfClass:[NSString class]])
        {

            if ([string isEqualToString:@"_id"] && [[xmlDict valueForKey:string] isKindOfClass:[NSString class]])
            {
                _lblID.text = [xmlDict valueForKey:@"_id"];
            }
            else
           {
               NSLog(@"%@", NSStringFromClass([[xmlDict valueForKey:string] class]));
           }
            NSLog(@"Model: %@ ", [xmlDict valueForKey:string]);

          //Set here other labels
        }
        else if ([[xmlDict valueForKey:string] isKindOfClass:[NSArray class]])
        {
            for (NSDictionary *dictionary in [xmlDict valueForKey:string]) {

                [self fetchValuesFromDictioanry:dictionary];
            }
        }
    }
}

你可以这样调用,

[self fetchValuesFromDictioanry:xmlDoc];

【讨论】:

  • 兄弟我已经解析了文件,问题是我得到的字典我想从中提取值。 @Hitesh Sultaniya
  • @Nabeel 当您将字典传递给此函数时,您将能够获取所需的所有值,对不起方法名称,但这只会遍历您的字典并在何时打印值key 的任何值都是字符串,你也不会得到任何错误
  • 如何在视图加载时调用此方法? @Hitesh Sultaniya
  • @Nabeel 请检查更新的答案,但参数应该是字典
  • 兄弟,它只显示字典中的 id 和 topic 的值,而对于其他它显示为 null。 @Hitesh Sultaniya
猜你喜欢
  • 1970-01-01
  • 2018-09-28
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多