Objective-c casting

I have a dictionary object from which I am retrieving data. The field should be a string field, but sometimes all it contains is a number. I get information with:

NSString *post = [[temp objectAtIndex:i] valueForKey:@"POSTDESCRIPTION"];

      

So it goes into a string object. However, when I try to assign this to the cell text with:

cell.textLabel.text = post;

      

I am getting the following error:

'NSInvalidArgumentException', reason: '*** -[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x4106a80'
2009-10-20 13:33:46.563

      

I've tried using it in the following ways: [/ p>

NSString *post = [[[temp objectAtIndex:i] valueForKey:@"POSTDESCRIPTION"] stringValue];
NSString *post = (NSString *)[[temp objectAtIndex:i] valueForKey:@"POSTDESCRIPTION"];
cell.textLabel.text = [post stringValue];
cell.textLabel.text = (NSSting *)post;

      

What am I doing wrong?

+2


source to share


2 answers


Your dictionary does not contain NSString

. If you want to get a string representation of an object, you can call an description

object selector like:



NSString *post = [[[temp objectAtIndex:i] valueForKey:@"POSTDESCRIPTION"] description];

      

+8


source


terry, jason and other answers and comments are correct. what you are trying will be like trying to turn an apple orange.

conveniently NSNumber has a stringValue method. so try this:

NSString *post = [[[temp objectAtIndex:i] valueForKey:@"POSTDESCRIPTION"] stringValue];

      



only do this if you know for sure that it will always be NSNumber.

Otherwise, you can try pretty hacks and unintelligent:

NSString *post = [NSString stringWithFormat:@"%@",[[[temp objectAtIndex:i] valueForKey:@"POSTDESCRIPTION"] description];

      

0


source







All Articles