0

Getting an NSException error. Looked around and it may be how I am calling the method but can't troubleshoot it, I'm a beginner so a good explanation is very much appreciated.

Here is my AddListingViewController.h

#import <UIKit/UIKit.h>
#import "ListingTableViewController.h"
#import "ListingManager.h"

@interface AddListingViewController : UIViewController
@property (nonatomic) ListingTableViewController *manager;
@property (nonatomic) ListingManager *add;

@end

Here is my AddListingViewController.m

#import "AddListingViewController.h"

@interface AddListingViewController ()
@property (weak, nonatomic) IBOutlet UITextField *title;
@property (weak, nonatomic) IBOutlet UITextView *desc;
@property (weak, nonatomic) IBOutlet UITextField *price;

@end

@implementation AddListingViewController


@synthesize manager = _manager;
@synthesize add = _add;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.manager = [[ListingTableViewController alloc] init];
    self.add = [[ListingManager alloc] init];
}

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

    //cancel posting on tap
- (IBAction)cancelListing:(UIBarButtonItem *)sender {
    NSLog(@"cancel tapped thpugh");
    [self dismissViewControllerAnimated:YES completion:nil];

}

    //add item on tap
- (IBAction)addListing:(UIBarButtonItem *)sender {
    NSLog(@"Add button tapped");

    self.add.listingTitle = self.title.text;
    self.add.listingDescription = self.desc.text;
    self.add.listingPrice = self.price.text;

    [self.manager.listings addObject:self.add];

    [self dismissViewControllerAnimated:YES completion:nil];

}

@end

The error I am getting (I'm sure it says is clearly here but I don't know how to troubleshoot it)

2014-06-30 21:37:44.825 Wildcat Exchange[1981:180450] Add button tapped
2014-06-30 21:37:44.827 Wildcat Exchange[1981:180450] -[UITextInputTraits text]: unrecognized selector sent to instance 0xc193e90
2014-06-30 21:37:44.831 Wildcat Exchange[1981:180450] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITextInputTraits text]: unrecognized selector sent to instance 0xc193e90'
suntzu
  • 65
  • 3
  • 13
  • Add an objective-c exception breakpoint and see what line of your code is triggering the exception. Best guess is that you have something mis-wired in your XIB file. – bbum Jul 01 '14 at 05:24

2 Answers2

0

First the exception. The exception is an NSInvalidArgumentException, which most often means you tried to call a function or method that doesn't exist on the object you tried to call it on, it could also be the case that an erroneous parameter was passed to a function.

Imagine for example you write a function that takes a string and turns it into an integer (lets ignore the fact that NSString already provides this). What should happen if someone passes in a string that can't be converted, say they pass the string "hello world". It's possible that your function would decide it can't do anything and so throws an InvalidArgmentException (this case is more rare in the Cocoa world and most often nil is returned instead of an exception being thrown).

In this case however it's the former and the error goes on further to say "-[UITextInputTraits text]: unrecognized selector sent to instance 0xc193e90". The part "unrecognized selector sent to instance" means just confirms what we think NSInvalidArgumentException means, that you tried to call a method (otherwise known as a selector) to some object stored at memory address 0xc193e90. What kind of object is at that address and what method did you call? That's the -[UITextInputTraits text]: part, it means you had an object of type UITextInputTraits and you tried to call the 'text' method on it.

Next step is finding where the crash occurred. To do this you need to set a global exception breakpoint (though given your crash happened right after you logged "Add button tapped" it likely on the following line, though we aren't sure. I'm not going to beat a dead horse on this one, it's been asked before and explained by Apple.

Once you have an exception breakpoint set Xcode will stop your app on the line that caused the exception to be thrown. At this point you can use some debugger commands or look at the surrounding code to figure out more. For example if it breaks on the third line in your addListing function it might be useful to know the kind of class so at the lldb console prompt you might try

po self.title

or 

po [self.title class]

If it's not a UITextfield or UITextView then you may have something wired incorrectly in you storyboard on XIB file, in which case you should check the connections inspector in interface builder.

Good luck

Community
  • 1
  • 1
Brandon
  • 2,387
  • 13
  • 25
  • Thanks, that not only answers my question but also shows how to troubleshoot things like this. The problem was actually the .text at the end. I made a NSString property and assigned that to self.aLabel.text for each one. – suntzu Jul 02 '14 at 03:41
-1

It looks like you're calling text when the name of the selector is text: (with the colon). Try adding the colon at the end and see if that works

Alex J
  • 1,029
  • 6
  • 8
  • I think I am doing that part right. See my comment above if you want to see how it was solved – suntzu Jul 02 '14 at 03:42