views:

93

answers:

3

How do I get the text value of an object in order to display it in a table? Other posts say objects are not NSStrings and you need to ask the object for its text. But how? The error is this:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[Names isEqualToString:]: unrecognized selector sent to instance 0xf51b60'

for this code in Objective-C:

NSString *cellValue = [namesArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;

My apologies to all you advanced programers, I'm new at this and could not find the answer in the NSObject documentation either. Thanks.

A: 

When you want to display an object with NSLog() and the "%@" format, the invoqued method is -(NSString*) description if it exists. It is your responsibility to write this method for your own classes.

mouviciel
Thanks. Will attempt and see what happens.
Doug
+3  A: 

You can use the -description method to get the string value of any object, similar to toString() in Java. Many classes also respond to the -stringValue method.

However, -description is used primarily for debugging purposes. If the code you posted isn't working that would mean namesArray contains something other than NSString instances, in which case you will need to find out what kind of objects are in your array; you can't just cast an object to NSString.

Darren
Originally, the code worked when I had string names in the code and they displayed properly in the table. But I've removed the string names now and am attempting to use CoreData where the user inputs names, they get saved, fetched, and then displayed in the table. Yes, the namesArray is supposed to contain strings, so maybe the its a CoreData problem that isn't working yet, or I need a stringValue method.
Doug
According the error message you have a class called "Names" that you're treating as a string, hence the error.
Darren
+2  A: 

I feel the answers so far are missing the point of the question. Your error is saying that the Names class doesn't implement isEqualToString: method. This is being called (presumably) because the UITableViewCell only changes the text of its textLabel if the string is different from what's being displayed, and it's performing this comparison using isEqualToString:, because cell.textLabel.text is an NSString property.

However, you're not giving it an NSString. You're giving it a Names object, so of course it's not going to work. Since Names is obviously a custom object, you must provide a method to extract a string representation from this object, and you must explicitly invoke that method yourself.

For example, you might implement a method called -asString (which would be a horrible name, but this is to illustrate a point), that might look something like this:

- (NSString*) asString {
  return [NSString stringWithFormat:@"This name is %@", aNameIvar];
}

You would then use it like this:

Name * thisName = [namesArray objectAtIndex:indexPath.row];
cell.textLabel.text = [thisName asString];
return cell;

The proper name for this method would be -stringValue. -stringValue is used on several Cocoa objects to return a string representation of the data that they hold, such as NSNumber, NSCell (Mac), etc.

EDIT:

Peter Hosey answered this exact question quite deftly in this StackOverflow.com question.

Dave DeLong
I did see Peter Hosey's answer before posting here. Very similar situation, but I got tripped up when his answer was "ask the object for its text". I'll try your suggestion for the -stringValue method and see if that works. Thanks. Also see my response to Darren; this used to work when I specifically defined the strings, but now am attempting to use CoreData instead.
Doug