将NSDictionary值转换为NSString

时间:2014-02-21 15:47:23

标签: ios nsdictionary

我想将字典的值转换为字符串。 我的代码:

NSArray *keys=[services allKeys];
yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(146, (i*height_between_cells)+44, 40, 25)];
    NSString *nomber = (NSString *)[[services objectForKey:[keys objectAtIndex: i]] objectForKey:@"Nomber"];
    //NSString *nomberService=[[services objectForKey:[keys objectAtIndex: i]] objectForKey:@"Nomber"];
    [yourLabel setText:nomber];
    [recapView addSubview:yourLabel];

就像你看到我想用我的dictionnary值设置标签的文本。 当我这样做时,我有一个错误:

[__NSCFNumber length]: unrecognized selector sent to instance 0x14d57fd0

有你的想法吗?

2 个答案:

答案 0 :(得分:2)

错误是因为您假设[[services objectForKey:[keys objectAtIndex: i]] objectForKey:@"Nomber"];的结果为您提供了NSString,但事实上它正在为您提供NSNumber

仅使用强制转换使编译器感到满意。它实际上并没有转换任何东西。

尝试这样的事情:

NSNumber *nomber = [[services objectForKey:[keys objectAtIndex: i]] objectForKey:@"Nomber"];
[yourLabel setText:[nomber stringValue]];

或者使用现代语法,您可以这样做:

NSNumber *nomber = services[keys[i]][@"Number"];
yourLabel.text = [nomber stringValue];

答案 1 :(得分:0)

NSString *nomberService=[[[services objectForKey:[keys objectAtIndex: i]] objectForKey:@"Nomber"] stringValue];