【问题标题】:NSDictionary to custom classNSDictionary 到自定义类
【发布时间】:2015-11-22 08:21:33
【问题描述】:

我有一个自定义类 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

谢谢

【问题讨论】:

    标签: nsdictionary quickblox nscoding nscopying nsarchiving


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 感谢您的回答,但是如果它的类没有这种方法怎么办?喜欢 QBChatMessage。
    • 你说的是什么方法?在里面?如果是,那也没关系。如果 QBChatDialog 自定义初始化是只读属性的必要原因,据我所知,QBChatMessage 没有这样的属性。
    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    相关资源
    最近更新 更多