gpt4 book ai didi

ios自定义类初始化不工作

转载 作者:行者123 更新时间:2023-11-28 21:31:38 26 4
gpt4 key购买 nike

我是 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;

//圣训.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;
}

我做错了什么?有什么遗漏吗?任何帮助将不胜感激。提前致谢:)

最佳答案

试试这个

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

关于ios自定义类初始化不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35794694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com