ios自定义类Init无法正常工作

时间:2016-03-04 11:16:59

标签: ios objective-c sqlite

我是iOS的初学者。我试图从我的应用程序中访问/读取sqlite数据库,我使用模型类初始化自定义初始化程序。值在自定义初始值设定项中成功,但我的问题是值不在模态类对象中填充。 这是我实现的代码:

// ViewController.m

{
[self thehadithList];
sqlite3 *database;
NSString *databasePath;

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:@"hadithQudsi.db"];


if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 

    const char *sqlStatement = "select * from hadith";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {  


            NSInteger aID = sqlite3_column_int (compiledStatement, 0);

            NSString *aHA = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
            NSString *aHE = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

            NSString *aRA = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
            NSString *aRE = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];

            NSInteger aF = sqlite3_column_int (compiledStatement, 5);


Hadith *hadith1 = [[Hadith alloc] initWithId:aID hadith_ar:aHA hadith_en:aHE ref_ar:aRA ref_en:aRE favourites:aF];
            [thehadithList addObject:hadith1]; **// **** Here in Custom class object, values are not been getting **** //**

        }
    }

    sqlite3_finalize(compiledStatement); 

}
sqlite3_close(database);

}

// Hadith.h(这是模态类)

@property (nonatomic,readwrite) NSInteger _id;
@property (nonatomic,strong) NSString *hadith_ar;
@property (nonatomic,strong) NSString *hadith_en;
@property (nonatomic,strong) NSString *ref_ar;
@property (nonatomic,strong) NSString *ref_en;
@property (nonatomic,readwrite) NSInteger  favourites;

-(id)initWithId:(NSInteger)ID hadith_ar:(NSString *)ha hadith_en:(NSString *)he ref_ar:(NSString *)ra ref_en:(NSString *)re  favourites:(NSInteger)f;

// Hadith.m

@synthesize _id,hadith_ar,hadith_en,ref_ar,ref_en,favourites;

-(id)initWithId:(NSInteger)ID hadith_ar:(NSString *)ha hadith_en:(NSString *)he ref_ar:(NSString *)ra ref_en:(NSString *)re  favourites:(NSInteger )f
{

if (self =  [super init]) {

    self._id = ID;
    self.hadith_ar = ha;
    self.hadith_en = he;

    self.ref_ar = ra;
    self.ref_en = re;
    self.favourites = f;
}



return self;
}

我做错了什么?有什么遗漏?任何帮助将不胜感激。在此先感谢:)

2 个答案:

答案 0 :(得分:1)

试试这个

    -(id)initWithId:(NSInteger)ID hadith_ar:(NSString *)ha hadith_en:(NSString *)he ref_ar:(NSString *)ra ref_en:(NSString *)re  favourites:(NSInteger )f
    {

    self =  [super init];
        self._id = ID;
        self.hadith_ar = ha;
        self.hadith_en = he;

        self.ref_ar = ra;
        self.ref_en = re;
        self.favourites = f;




return self;
}

答案 1 :(得分:1)

试试这个

-(id)initWithId:(NSInteger)ID hadith_ar:(NSString *)ha hadith_en:(NSString *)he ref_ar:(NSString *)ra ref_en:(NSString *)re  favourites:(NSInteger )f
        {
        self =  [super init];
            __id = ID;
            _hadith_ar = ha;
            _hadith_en = he;
            _ref_ar = ra;
            _ref_en = re;
            _favourites = f;
    return self;
    }