문제

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:]
도움이 되었습니까?

해결책

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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top