NSDictionary to custom class

79 Views Asked by At

I've a custom class QBChatDialog object, that I'm storing in sqlite database like

  -(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.

// 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 . The class is third party API, and some properties are read-only.

Thanks

1

There are 1 best solutions below

2
On

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:

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, e.g.:

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