Question

I'm trying to set

cell.detailTextLabel.text = [object valueForKeyPath:"@foo"]

Equal to a number. I can set a string in this field but not a number.

But my error is:

  • Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]
Was it helpful?

Solution

You can't directly assign an NSNumber to an NSString value (the label's text property).

You need to convert the number to a string first.

The best option is to use an NSNumberFormatter so the number appears properly given the user's locale.

NSNumber *val = object[@"foo"];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
// configure the formatter as needed
NSString *str = [formatter stringFromNumber:val];
cell.detailTextLabel.text = str;

Though you may get acceptable results by simply doing:

cell.detailTextLabel.text = [object[@"foo"] stringValue];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top