NSDictionary到自定义类

时间:2015-11-22 08:21:34

标签: nsdictionary quickblox nscoding nscopying nsarchiving

我有一个自定义类QBChatDialog对象,我将其存储在sqlite数据库中

  -(void)storeInDB:(QBChatDialog *)dialog {    
         NSString *query = = [NSString stringWithFormat:@"INSERT INTO dialogs (dialog_id,last_message) VALUES ('%@','%@')",dialog.ID,dialog.lastMessageText];
        //run the query
}

然后我从数据库中检索为NSDictionary。

// after fetching as an array in dbrecord 
NSDictionary *dialogDictionary = @{@"dialog_id":[dbrecord objectAtIndex:DIALOG_ID_INDEX],
                                 @"dialog_last_message":dbrecord objectAtIndex:DIALOG_LAST_MESSAGE_INDEX]
                                  };

如何将其映射回QBChatDialog课程,以获取dialog.IDdialog.lastMessageText等值。该类是第三方API,有些属性是read-only

由于

1 个答案:

答案 0 :(得分:0)

您不需要设置只读属性,因此您基本上可以解开NSDictionary,只需确保您确定存储对话框ID及其类型,以便您可以从以下代码开始:< / p>

QBChatDialog *fetchedDialog = [[QBChatDialog alloc] initWithDialogID:dialogDictionary[@"dialog_id"] type:dialogDictionary[@"dialog_type"]];

然后只需设置你需要的每个字段,这不是只读的,例如:

fetchedDialog.lastMessageText = dialogDictionary[@"dialog_last_message"];