编译代码时收到以下错误。我正在尝试连接到一个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数据库和其他代码。
#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;
}
答案 0 :(得分:2)
您正在呼叫initWithDatabaseFile:
,但该方法的名称为initWithDatabaseFilename:
。
确保DBManager
的.h文件具有与.m文件中的实际方法匹配的正确名称。