开发者

Casting in objective-c

I have a dictionary object that I am pulling data out of. The field is开发者_运维知识库 supposed to be a string field but sometime all that it contains is a number. I get the info using:

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

So it is going into a string object. However, when I try to assign that to a cell's text via:

cell.textLabel.text = post;

I get a the following error:

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

I have tried casting it with the following ways to no avail:

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?


Your dictionary doesn't contain an NSString. If you'd like the string representation of the object, you could call the object's description selector, e.g.:

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


terry, jason and the other answers & comments are correct. what you're attempting would be like trying to cast an apple into an orange.

conveniently, NSNumber does have a stringValue method. so try this:

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

only do this if you know for sure it'll always be an NSNumber.

otherwise, you can try the rather hacky and inelligant:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜