József Vesza
József Vesza

Reputation: 4795

[__NSCFNumber length]: unrecognized selector sent to instance

I am trying to pass data from a table view controller to a detail view controller. Every entry of my table works just as it should, except for one. Here is the prepareForSegue method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([[segue identifier] isEqualToString:@"showDetails"]) {
        NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
        NSDictionary *notification = [[self notifications] objectAtIndex:[indexPath row]];
        NRDetailViewController *destViewController = [segue destinationViewController];
        [destViewController setDate:[notification objectForKey:@"date"]];
        [destViewController setFrom:[notification objectForKey:@"from"]];
        [destViewController setIden:[notification objectForKey:@"identifier"]];
        [destViewController setPriority:[notification objectForKey:@"priority"]];
        [destViewController setSubject:[notification objectForKey:@"subject"]];
        [destViewController setMessage:[notification objectForKey:@"text"]];

    }
}

Here is my interface for the detail view:

#import <UIKit/UIKit.h>

@interface NRDetailViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *dateField;
@property (weak, nonatomic) IBOutlet UITextField *fromField;
@property (weak, nonatomic) IBOutlet UITextField *idenField;

@property (weak, nonatomic) IBOutlet UITextField *prioField;
@property (weak, nonatomic) IBOutlet UITextField *subjectField;
@property (weak, nonatomic) IBOutlet UITextView *messageView;


@property (copy, nonatomic) NSString *date;
@property (copy, nonatomic) NSString *from;
@property (copy, nonatomic) NSString *iden;
@property (copy, nonatomic) NSString *priority;
@property (copy, nonatomic) NSString *subject;
@property (copy, nonatomic) NSString *message;


@end

Here is the detail view's implementation file:

#import "NRDetailViewController.h"

@interface NRDetailViewController ()

@end

@implementation NRDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self dateField] setText:[self date]];
    [[self fromField] setText:[self from]];
    [[self idenField] setText:[self iden]];
    [[self prioField] setText:[self priority]];
    [[self subjectField] setText:[self subject]];
    [[self messageView] setText:[self message]];



}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I have problem setting the text of idenField to the text of iden. I have added an exception breakpoint, and got that the exception indeed occurs at this line:

[[self idenField] setText:[self iden]];

At this point, I have printed the value of [self iden] and it even contains the content I want to pass, so I have absolutely no idea what the problem is, since as I said, all the other fields are working as they should.

The exception being thrown is:

2013-08-07 22:28:20.539 NotificationReader[1214:11303] -[__NSCFNumber length]: unrecognized selector sent to instance 0x7587a00

Any help would be appreciated greatly.

Upvotes: 2

Views: 12431

Answers (3)

Gustavo Barbosa
Gustavo Barbosa

Reputation: 1360

To set the text, iden needs to be a NSString. When you are getting the object from the NSDictionary, it is a NSNumber.

Try this:

[destViewController setIden:(NSString *)[notification objectForKey:@"identifier"]];

or change the iden property to be a NSNumber and then

[[self idenField] setText:[NSString stringWithFormat:@"%d", [self iden]]];    

Upvotes: 1

Abdullah Shafique
Abdullah Shafique

Reputation: 6918

Somewhere you are passing an NSNumber instead of a NSString.

Upvotes: 2

Jake Spencer
Jake Spencer

Reputation: 1127

It looks like:

[destViewController setIden:[notification objectForKey:@"identifier"]];

is returning an NSNumber and not an NSString. You could try replacing that line with:

[destViewController setIden:[(NSNumber*)[notification objectForKey:@"identifier"] stringValue]];

Upvotes: 7

Related Questions