1

I'm displaying an array of dictionaries that comes from web service, in a table. This is the code snippet in cellForRowAtIndexPath.

cell.businessNameLabel.text = [dataDict objectForKey:@"business_name"];
[cell.businessNameLabel setTextColor:[UIColor colorWithRed:1.0/255.0 green:135.0/255.0 blue:68.0/255.0 alpha:1]];
UIFont *customfont = [UIFont fontWithName:@"MyriadPro-Bold" size:16];
[cell.businessNameLabel setFont:customfont];

cell.serviceTypeLabel.text = [dataDict objectForKey:@"address"];
[cell.serviceTypeLabel setTextColor:[UIColor blackColor]];
customfont = [UIFont fontWithName:@"MyriadPro-Regular" size:11];
[cell.serviceTypeLabel setFont:customfont];

cell.businessID = [dataDict objectForKey:@"business_id"];
cell.reviewScoreLabel.text = [dataDict objectForKey:@"rating"];

return cell;

The app crashes on the following line:

cell.reviewScoreLabel.text = [dataDict objectForKey:@"rating"];

With the following exception detail:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0xb000000000000003'

The dictionary object data looks something like this:

address = "Boston, MA, United States";
"business_id" = 15;
"business_image" = "http://demo.web.com/home-business/upload/user/1420452418_.jpeg";
"business_name" = autodealersma;
lat = "42.3584865";
lng = "-71.06009699999998";
rating = "4.3";    
service = "auto dealers";
Larme
  • 24,190
  • 6
  • 51
  • 81
  • 1
    The "4.3" seems to be a `NSNumber` and not a `NSString`. – Larme Jan 21 '15 at 10:54
  • I think it is because you are trying to set an NSNumber to a text property. The text property needs a NSString. Could you try: `cell.reviewScoreLabel.text = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"rating"]];` Please see my answer below. – Menno Jan 21 '15 at 10:56

3 Answers3

1

I think it is because you are trying to set an NSNumber to a text property. The text property needs a NSString. Could you try:

cell.reviewScoreLabel.text = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"rating"]];

Menno
  • 138
  • 1
  • 7
0

try this ...

cell.businessNameLabel.text = [[dataDict objectForKey:@"business_name"]objectAtIndex:indexPath.row];
[cell.businessNameLabel setTextColor:[UIColor colorWithRed:1.0/255.0 green:135.0/255.0 blue:68.0/255.0 alpha:1]];
UIFont *customfont = [UIFont fontWithName:@"MyriadPro-Bold" size:16];
[cell.businessNameLabel setFont:customfont];

cell.serviceTypeLabel.text = [[dataDict objectForKey:@"address"]objectAtIndex:indexPath.row];
[cell.serviceTypeLabel setTextColor:[UIColor blackColor]];
customfont = [UIFont fontWithName:@"MyriadPro-Regular" size:11];
[cell.serviceTypeLabel setFont:customfont];

cell.businessID = [[dataDict objectForKey:@"business_id"]objectAtIndex:indexPath.row];
cell.reviewScoreLabel.text = [[dataDict objectForKey:@"rating"]objectAtIndex:indexPath.row];

return cell;
Sam
  • 198
  • 8
0

Modify your code to

cell.businessNameLabel.text = [NSString stringWithFormat:@"%@", [dataDict objectForKey:@"business_name"]];
[cell.businessNameLabel setTextColor:[UIColor colorWithRed:1.0/255.0 green:135.0/255.0 blue:68.0/255.0 alpha:1]];
UIFont *customfont = [UIFont fontWithName:@"MyriadPro-Bold" size:16];
[cell.businessNameLabel setFont:customfont];

cell.serviceTypeLabel.text = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"address"]];
[cell.serviceTypeLabel setTextColor:[UIColor blackColor]];
customfont = [UIFont fontWithName:@"MyriadPro-Regular" size:11];
[cell.serviceTypeLabel setFont:customfont];

cell.businessID = [dataDict objectForKey:@"business_id"];
cell.reviewScoreLabel.text = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"rating"]];

return cell;

The reason of the crash must be this, the object you are trying to set to your label may not be a NSString. So better take StringValue or format it.

Sudhin Davis
  • 2,010
  • 13
  • 18