NSException 的终止异常类型
Posted
技术标签:
【中文标题】NSException 的终止异常类型【英文标题】:Terminating exception type of NSException 【发布时间】:2014-12-13 17:32:43 【问题描述】:我在编译代码时收到以下错误。我正在尝试连接到 sqlite 3 数据库,并在将文本输入 UIAlertView 后将其保存到表中,创建一个新列表。非常感谢任何和所有帮助我花了几个小时试图弄清楚为什么会抛出这个错误,因为sql 文件在项目中,是使用 sqlitebrowser 创建的。
2014-12-04 19:10:29.917 SmartShop[7964:60b] -[DBManager initWithDatabaseFile:]: unrecognized selector sent to instance 0x109239910
2014-12-04 19:10:29.925 SmartShop[7964:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DBManager initWithDatabaseFile:]: unrecognized selector sent to instance 0x109239910'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101a35495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010179499e objc_exception_throw + 43
2 CoreFoundation 0x0000000101ac665d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000101a26d8d ___forwarding___ + 973
4 CoreFoundation 0x0000000101a26938 _CF_forwarding_prep_0 + 120
5 SmartShop 0x0000000100001c62 -[ViewController viewDidLoad] + 114
6 UIKit 0x000000010043759e -[UIViewController loadViewIfRequired] + 562
7 UIKit 0x0000000100437777 -[UIViewController view] + 29
8 UIKit 0x00000001007422e2 -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 390
9 UIKit 0x000000010037dffa -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 1109
10 UIKit 0x000000010037db9f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36
11 UIKit 0x000000010037daef -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 101
12 UIKit 0x000000010037cdfe -[UIWindow _updateToInterfaceOrientation:duration:force:] + 377
13 UIKit 0x000000010043b70a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 147
14 UIKit 0x0000000100377b1b -[UIWindow addRootViewControllerViewIfPossible] + 490
15 UIKit 0x0000000100377c70 -[UIWindow _setHidden:forced:] + 282
16 UIKit 0x0000000100380ffa -[UIWindow makeKeyAndVisible] + 51
17 UIKit 0x000000010033cc98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1788
18 UIKit 0x0000000100340a0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
19 UIKit 0x0000000100351d4c -[UIApplication handleEvent:withNewEvent:] + 3189
20 UIKit 0x0000000100352216 -[UIApplication sendEvent:] + 79
21 UIKit 0x0000000100342086 _UIApplicationHandleEvent + 578
22 GraphicsServices 0x0000000103bae71a _PurpleEventCallback + 762
23 GraphicsServices 0x0000000103bae1e1 PurpleEventCallback + 35
24 CoreFoundation 0x00000001019b7679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
25 CoreFoundation 0x00000001019b744e __CFRunLoopDoSource1 + 478
26 CoreFoundation 0x00000001019e0903 __CFRunLoopRun + 1939
27 CoreFoundation 0x00000001019dfd83 CFRunLoopRunSpecific + 467
28 UIKit 0x00000001003402e1 -[UIApplication _run] + 609
29 UIKit 0x0000000100341e33 UIApplicationMain + 1010
30 SmartShop 0x0000000100002563 main + 115
31 libdyld.dylib 0x0000000101fba5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
下面是我在 ViewController 中引用 SQLite3 db 和其他代码的代码。
#import "ViewController.h"
#import "DBManager.h"
@interface ViewController ()
@property (nonatomic, strong) DBManager *dbManager;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Initialize the dbManager object.
self.dbManager = [[DBManager alloc] initWithDatabaseFile:@"shop.sql.sqbpro"];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (IBAction)addNewList:(id)sender
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add List" message:@"Enter new list name:" delegate:self cancelButtonTitle:@"Add" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *alertTextField = [alert textFieldAtIndex:0];
alertTextField.placeholder = @"List name";
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
NSString *listName = [[alertView textFieldAtIndex:0] text];
NSLog(@"ENTERED: %@",listName);
//Prepare the query String
NSString *query = [NSString stringWithFormat:@"insert into lists values(null, '%@')", listName];
//Execute the query
[self.dbManager executeQuery:query];
//If successful pop the view controller
if(self.dbManager.affectedRows != 0)
NSLog(@"Query was executed successfully. Affected rows = %d", self.dbManager.affectedRows);
//Pop view controller
[self.navigationController popViewControllerAnimated:YES];
else
NSLog(@"Could not execute the query.");
@end
最后是我的 initWithDatabaseFileName 代码:
-(instancetype)initWithDatabaseFilename:(NSString *)dbFilename
self = [super init];
if (self)
// Set the documents directory path to the documentsDirectory property.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.documentsDirectory = [paths objectAtIndex:0];
// Keep the database filename.
self.databaseFilename = dbFilename;
// Copy the database file into the documents directory if necessary.
[self copyDatabaseIntoDocumentsDirectory];
return self;
【问题讨论】:
【参考方案1】:您正在调用initWithDatabaseFile:
,但方法的名称是initWithDatabaseFilename:
。
确保 DBManager
的 .h 文件具有与 .m 文件中的实际方法匹配的正确名称。
【讨论】:
就是这样,头文件引用了错误的方法名称。谢谢你指出这么简单的事情。我想我看得太久了,错过了简单的答案。非常感谢,非常感谢!以上是关于NSException 的终止异常类型的主要内容,如果未能解决你的问题,请参考以下文章
Swift:以 NSException 类型的未捕获异常终止
Swift 项目以 NSException 类型的未捕获异常终止
以 NSException 类型的未捕获异常终止 - Xcode