2014-12-13 8 views
1

При компиляции моего кода возникает следующая ошибка. Я пытаюсь подключиться к базе данных sqlite 3 и по тексту, введенному в UIAlertView, сохранить это в таблицу, создав новый список. Все и вся помощь очень ценятся. Я потратил часы, пытаясь понять, почему эта ошибка возникает, потому что sql-файл находится в проекте и был создан с использованием sqlitebrowser.Завершение типа исключения NSException

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; 
} 

ответ

2

Вы вызываете initWithDatabaseFile: но имя метода initWithDatabaseFilename:.

Убедитесь, что ваш .h-файл для DBManager имеет правильное имя, соответствующее фактическому методу в файле .m.

+0

Именно так, файл заголовка ссылался на неправильное имя метода. Спасибо, что заметили что-то настолько простое. Наверное, я слишком долго смотрел на нее и пропустил легкий ответ. Большое спасибо, это очень ценится !! – chrisotto6

Смежные вопросы