0

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

Sikander
  • 447
  • 4
  • 26

1 Answers1

0

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"];
Raikerian
  • 552
  • 2
  • 7
  • Thanks for the answer, but what if its a class that doesn't have this kind of method ? Like QBChatMessage. – Sikander Nov 24 '15 at 16:11
  • What method are you talking about? init? If yes, then it doesn't matter. In case of QBChatDialog custom init is necessary cause of readonly properties, as far as I know, QBChatMessage doesn't have such properties. – Raikerian Nov 24 '15 at 21:09