Gauthier Beignie
Gauthier Beignie

Reputation: 161

Convert a NSDictionary value to a NSString

i want to convert the value of my dictionary into string. My code :

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];

like you see i want to set the text of the label with my dictionnary value. When i do that i have an error :

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

Have you an idea ?

Upvotes: 0

Views: 656

Answers (2)

rmaddy
rmaddy

Reputation: 318794

The error is because you are assuming that the result of [[services objectForKey:[keys objectAtIndex: i]] objectForKey:@"Nomber"]; is giving you an NSString but it is in fact giving you an NSNumber.

Using a cast only makes the compiler happy. It doesn't actually convert anything.

Try something like this:

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

Or using modern syntax you can do:

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

Upvotes: 2

chawki
chawki

Reputation: 887

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

Upvotes: 0

Related Questions