views:

1324

answers:

3

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?

A: 

I think you're pulling a NSDecimalNumber instead of a string. Are you sure you're not converting before storing the value in the NSDictionary?

Rui Pacheco
+4  A: 

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];
Jason
In other words: There is no such thing as casting objects in Objective-C.
Chuck
There certainly is casting of objects in Objective-C. You just can't cast an object to something that it's not. And and NSNumber is not an NNSString.
Terry Wilcox
@Terry: No, what I said was correct. You can't cast an object in Objective-C. You can cast *pointers* from one type to another, but that doesn't typecast the object being pointed to.
Chuck
You can (type) cast objects in Objective-C: `NSString *string = @"Hi";` `NSNumber *number = (NSNumber *)string;`. However, type-casting like this does not "convert" the object to the type-casted type. Type-casting like this is generally frowned on and will likely run in to C's `undefined behavior` at some point, usually long after you wrote it.
johne
@Chuck: Ah, I see what you're saying. The thought of expecting casting to change the underlying object never occurred to me.
Terry Wilcox
A: 

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];
pxl
There is no point at all to the `NSString stringWithFormat:@"%@",...` in your last example. Just calling `description` gives you a string, and using a `%@` specifier in `stringWithFormat:` calls `description` on that argument, which in the case of an NSString just returns `self`.
Chuck
true, i got overzealous, [[temp objectAtIndex:i] valueForKey:@"POSTDESCRIPTION"] would've sufficed.
pxl