如何将 sqlite3 table 分配给 nsarray

how to assign a sqlite3 table to nsarray

我正在开发一个聊天应用程序,因为我使用 sqlite 来存储和检索聊天记录我能够插入值但是在检索时我得到数组中的 nil 这是我的代码

messageArray=[DBObject AllRowFromTableName:@"chatTable" withUserID:myJID withFriendID:chatWithUser];


for (int i=0; i<[messageArray count]; i++) {

    Message *msg=[[Message alloc] init];
          msg.text=[[messageArray objectAtIndex:i] msg];
    if ([[messageArray objectAtIndex:i] lft_rght]==0) {
        msg.fromMe=YES;
    }
    else{
        msg.fromMe=NO;
    }

    msg.type=SOMessageTypeText;

    NSString *dateString =[[messageArray objectAtIndex:i] tim_dte];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm,yyyy/MM/dd"];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:dateString];
    msg.date=dateFromString;

    [self.chatHistoryArray addObject:msg];

这是我正在检索的 class 广告数据库 class 是

 -(NSMutableArray *)AllRowFromTableName:(NSString *)tableName withUserID:(NSString *)userid withFriendID:(NSString *)friendid

   {
NSMutableArray *array=[[NSMutableArray alloc] init];

NSString *sqlString=[NSString stringWithFormat:@"select *from chatTable where toid='%@' and fromid='%@'",userid,friendid];

sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &statement, nil)==SQLITE_OK) {

    while (sqlite3_step(statement)==SQLITE_ROW) {
         Message *tempSkeleton=[[Message alloc] init];

        tempSkeleton.mToID =[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];

        tempSkeleton.mFromID=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
        tempSkeleton.text=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 3)];

        //tempSkeleton.fromMe=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
        tempSkeleton.fromMe=((int)sqlite3_column_int(statement, 4)) ?  YES : NO;
        tempSkeleton.tim_dte=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 5)];




        [array addObject:tempSkeleton];
        //  NSLog(@"IN LOOP=>%@",array);

    }



}

我收到 msg.text 空罐 body 请帮帮我

在处理返回数组的循环中,这些行...

Message *msg=[[Message alloc] init];
msg.text=[[messageArray objectAtIndex:i] msg];

应该只是...

Message *msg=[[Message alloc] init];
msg.text=[[messageArray objectAtIndex:i] text];

您还应该在循环外创建一次 NSDateFormatter 并使用它,因为它不是要创建的 "quick" 对象。

您可能还想重构对 [messageArray objectAtIndex:i] 的常量调用。创建一个局部变量,获取引用一次,然后使用它。所以重构后的代码看起来像...

messageArray=[DBObject AllRowFromTableName:@"chatTable" withUserID:myJID withFriendID:chatWithUser];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm,yyyy/MM/dd"];

for (int i=0; i<[messageArray count]; i++) {
    Message * baseMessage = [messageArray objectAtIndex:i];
    Message *msg=[[Message alloc] init];
    msg.text= baseMessage.text;
    msg.fromMe= (baseMessage.left_rght == 0);
    msg.type=SOMessageTypeText;
    msg.date=[dateFormatter dateFromString:baseMessage.tim_dte];

    [self.chatHistoryArray addObject:msg];
}

您还可以使用快速枚举器,也许可以查看现代 objective c https://developer.apple.com/library/ios/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html