【问题标题】:Getting NSException error获取 NSException 错误
【发布时间】:2014-07-01 02:44:19
【问题描述】:

得到一个 NSException 错误。环顾四周,这可能是我调用该方法的方式,但无法对其进行故障排除,我是初学者,非常感谢一个好的解释。

这是我的 AddListingViewController.h

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

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

@end

这是我的 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

我得到的错误(我确定它说得很清楚,但我不知道如何解决它)

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'

【问题讨论】:

  • 添加一个objective-c异常断点,看看你的代码的哪一行触发了异常。最好的猜测是您的 XIB 文件中有错误连接。

标签: objective-c ios7 nsexception


【解决方案1】:

首先是异常。异常是 NSInvalidArgumentException,这通常意味着您尝试调用的函数或方法在您尝试调用它的对象上不存在,也可能是错误的参数被传递给函数。

假设你编写了一个函数,它接受一个字符串并将其转换为一个整数(让我们忽略 NSString 已经提供这个的事实)。如果有人传入一个无法转换的字符串会发生什么,比如说他们传递了字符串“hello world”。您的函数可能会决定它不能做任何事情,因此抛出 InvalidArgmentException (这种情况在 Cocoa 世界中更为罕见,通常返回 nil 而不是抛出异常)。

在这种情况下,它是前者,错误继续说“-[UITextInputTraits text]:无法识别的选择器发送到实例 0xc193e90”。 “发送到实例的无法识别的选择器”部分意味着只是确认了我们认为 NSInvalidArgumentException 的含义,即您尝试对存储在内存地址 0xc193e90 的某个对象调用方法(也称为选择器)。那个地址是什么类型的对象,你调用了什么方法?这就是 -[UITextInputTraits text]: 部分,这意味着您有一个 UITextInputTraits 类型的对象,并且您尝试对其调用 'text' 方法。

下一步是查找崩溃发生的位置。为此,您需要设置一个全局异常断点(尽管考虑到您的崩溃发生在您登录“点击添加按钮”之后,它可能在下一行,但我们不确定。我不会打败死马在这一点上,有人问过before 并由Apple 解释。

一旦你设置了异常断点,Xcode 就会在导致异常被抛出的那一行停止你的应用程序。此时您可以使用一些调试器命令或查看周围的代码以了解更多信息。例如,如果它在您的 addListing 函数的第三行中断,那么了解类的类型可能很有用,因此您可以在 lldb 控制台提示符处尝试

po self.title

or 

po [self.title class]

如果它不是 UITextfield 或 UITextView,那么您可能在 XIB 文件的情节提要中连接错误,在这种情况下,您应该检查界面生成器中的连接检查器。

祝你好运

【讨论】:

  • 谢谢,这不仅回答了我的问题,而且还展示了如何解决此类问题。问题实际上是最后的 .text 。我创建了一个 NSString 属性并将其分配给每个 self.aLabel.text。
【解决方案2】:

看起来您在选择器的名称是文本时调用文本:(使用冒号)。尝试在末尾添加冒号,看看是否有效

【讨论】:

  • 我认为我做得对。如果您想了解它是如何解决的,请参阅我上面的评论
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多