converting NSDictionary object to NSData object and vice-versa

前端 未结 2 869
难免孤独
难免孤独 2020-12-02 14:51

I have to convert an NSDictionary object into NSData and further I have to get the same NSDictionary out of the NSData ob

2条回答
  •  萌比男神i
    2020-12-02 15:23

    A much simpler version of Robert's answer:

    [NSKeyedArchiver archiveRootObject:YOURDICTIONARY toFile:YOURFILEPATH];
    

    And, correspondingly:

    YOURDICTIONARY = [NSKeyedUnarchiver unarchiveObjectWithFile:YOURFILEPATH];
    

    Or to answer the question as originally set, without imputing a file into things:

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:YOURDICTIONARY];
    ...
    YOURDICTIONARY = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    

    It's all factory methods so it's the same code with or without ARC; the methods used have been available since OS X v10.2 and on iOS since day one.

提交回复
热议问题