简体   繁体   English

NSDictionary自定义类

[英]NSDictionary to custom class

I've a custom class QBChatDialog object, that I'm storing in sqlite database like 我有一个自定义类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
}

Then I'm retrieving as NSDictionary from database. 然后,我从数据库中检索为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]
                                  };

How can I map it back to QBChatDialog class, to get values like dialog.ID or dialog.lastMessageText . 我如何将其映射回QBChatDialog类,以获取诸如dialog.IDdialog.lastMessageText类的值。 The class is third party API, and some properties are read-only . 该类是第三方API,某些属性是read-only

Thanks 谢谢

You don't need to set readonly properties, so you can basically unwrap your NSDictionary, just make sure you for sure store dialog id and it's type, so that you can start with this code: 您不需要设置只读属性,因此您基本上可以解开NSDictionary,只需确保确定存储对话框ID及其类型即可,因此可以从以下代码开始:

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

And after that just set every field you need, that is not readonly, eg: 之后,只需设置所需的每个字段即可,这不是只读的,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM