检索sqlite数据时“无法识别的选择器发送到实例”

时间:2011-04-08 07:48:43

标签: iphone xcode sqlite

我一直收到此错误,并且不知道如何修复它。我是iOS编程的新手。

希望下面的代码足以看出问题所在。

这是我的 ListTableController.m


...
@implementation ListTableController

@synthesize listTableView;
@synthesize detailController;
@synthesize listOfTasks;
@synthesize title, note, date;

-(id)initWithTitle:(NSString *)sTitle note:(NSString *)sNote date:(NSString *)sDate {
    NSLog(@"FUNC: %s",__FUNCTION__);
    self.title = sTitle;
    self.note = sNote;
    self.date = sDate;
    return self;
}

// Retrieve records
-(void) getAllRowsFromTableNamed: (NSString *) tableName { 

    //---retrieve rows--- 
    NSString *qsql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE status = '1' ORDER BY date ASC",tableName]; sqlite3_stmt *statement;
    //---initialize the array--- 
    listOfTasks = [[NSMutableArray alloc] init];

    if (sqlite3_prepare_v2( db, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK) { 
        while (sqlite3_step(statement) == SQLITE_ROW) {
            NSString *fieldTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
            NSString *fieldNote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
            NSString *fieldDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];

            // Create a new object with the data from the database
            NSString *str = [[NSString alloc] initWithTitle :fieldTitle note:fieldNote date:fieldDate];
            [listOfTasks addObject:str];            
            [str release];
            [fieldTitle release]; 
            [fieldNote release]; 
            [fieldDate release];        
        }
        //---deletes the compiled statement from memory---
        sqlite3_finalize(statement);
    }
}
...

这是我的 ListTableController.h


#import 
#import "sqlite3.h"
#import "TaskDetailController.h"

@interface ListTableController : UITableViewController {
    IBOutlet UITableView *listTableView;
    sqlite3 *db;
    TaskDetailController *detailController;

}
NSString *title;
NSString *note;
NSString *date;
NSMutableArray * listOfTasks;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *note;
@property (nonatomic, retain) NSString *date;
@property (nonatomic, retain) TaskDetailController *detailController;
@property (nonatomic, retain) IBOutlet UITableView *listTableView;
@property (nonatomic, retain) NSMutableArray *listOfTasks;

-(void) copyDatabaseIfNeeded;
-(NSString *) filePath; 
-(IBAction)addCalendar;
-(NSInteger)countToday;
-(id)initWithTitle:(NSString *)sTitle note:(NSString *)sNote date:(NSString *)sDate;

@end

这是来自控制台的错误日志:


*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSPlaceholderString initWithTitle:note:date:]: unrecognized selector sent to instance 0x4e00470'

2 个答案:

答案 0 :(得分:0)

尝试删除这些行。类方法为[NSString stringWith ....返回自动释放的对象。

[fieldTitle release]; 
[fieldNote release]; 
[fieldDate release]; 

我认为你必须首先了解内存管理。如果你不这样做,你将会头疼。

答案 1 :(得分:0)

尝试删除这些行。

[fieldTitle release]; [fieldNote release]; [fieldDate release];

如果您想将标题,注释和日期合并为一行。

然后尝试这个

//使用数据库中的数据创建一个新对象 NSString * str = [[NSString stringWithFormate:@“%@%@%@”,fieldTitle,fieldNote,fieldDate];

而不是

//使用数据库中的数据创建一个新对象 NSString * str = [[NSString alloc] initWithTitle:fieldTitle note:fieldNote date:fieldDate];