unrecognized selector sent to instance ** plz help

I have a navigation controlled app that display's different table views as you navigate down the path. I decided that I wanted to show a CustomCell instead of the regular default cells provided for the grouped style table.

I created the CustomCell.h and CustomCell.m files along with the CustomCell.xib nib file.

I have done this MANY times before in other projects but this one will not let me set any of the fields on the CustomCell. I have stipped this cell down to just one data element called 'desc' and it is a UILabel I am trying to set.

Here is the CustomCell.h file and some of the .m file using the CustomCell class.


#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell {
IBOutlet UILabel *desc;
}
@property (nonatomic, retain) IBOutlet UILabel *desc;
@end



- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell2 = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
...do some logic
cell2.desc.text = [NSString stringWithFormat:@"%@", myText]; //ERROR OCCURS HERE
return cell2;


I receive the following error:


4/4/09 2:27:20 PM todo[1484] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSObject desc]: unrecognized selector sent to instance 0x5239b0'


The links are all set in IB and the CustomCell is set to be of Class "CustomCell"

Anyone have any idea why I cannot set the value of the UILabel for the cell2 object?

Thanks in advance

Mac Mini Core 2 Duo, Mac OS X (10.5.6)

Posted on Apr 4, 2009 12:38 PM

Reply
8 replies

Apr 4, 2009 3:12 PM in response to crivoli

It's never wise to publicly attempt an interpretation of ok's advice, but fools go where angels.. etc.

I think the recommendation was to make sure the object you got from your nib was an instance of the class you expected. This sounds like an especially good idea given the difference in behavior on the two platforms. For example:

if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
// debug code aka introspection
if ([cell2 isMemberOfClass:[CustomCell class]])
NSLog(@"%@ is CustomCell", cell2);
else {
NSLog (@"%@ is not CustomCell", cell2);
}
}

Apr 5, 2009 7:35 AM in response to RayNewbie

RayNewbie wrote:
I think the recommendation was to make sure the object you got from your nib was an instance of the class you expected. This sounds like an especially good idea given the difference in behavior on the two platforms. For example:

if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
// debug code aka introspection
if ([cell2 isMemberOfClass:[CustomCell class]])
NSLog(@"%@ is CustomCell", cell2);
else {
NSLog (@"%@ is not CustomCell", cell2);
}
}


That is fairly limiting and isn't good practice. You see that approach more in C++, which isn't as strong an object-oriented language as Objective-C. Even in C++ though, there are better ways to handle it.

In Objective-C, it is pretty easy. Look at it from the perspective of what you want to do. Then, you ask the object if it can do what you want via "respondsToSelector:"

Apr 5, 2009 11:41 AM in response to etresoft

Thanks a bunch, etresoft. I also needed some guidance when I tried to help with a related question last week: [http://discussions.apple.com/thread.jspa?messageID=9235322&#9235322]. In that case we were trying to identify NSMutableDictionary and we had the benefit of a warning in the docs (ok pointed me to the explanation so I learned we were dealing with the superclass of a class cluster--or at least I learned those words).

Now I'm wondering if the NSObject class method is ever useful.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

unrecognized selector sent to instance ** plz help

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.