Convert nsdictionary to nsdata

后端 未结 5 2096
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 19:38

have an app that can take a picture and then upload to a server. encoding it to base 64 and pass it thru a XMLRPC to my php server.

i want to take the NSDictionary info

5条回答
  •  抹茶落季
    2021-02-05 20:24

    Three options occur to me on this, two mentioned in other answers NSKeyedArchiver and PropertyList, there is also NSJSONSerialization that gave me the most compact data in a simple test.

    NSDictionary *dictionary = @{@"message":@"Message from a cool guy", @"flag":@1};
    NSData *prettyJson = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
    NSData *compactJson = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
    NSData *plist = [NSPropertyListSerialization dataWithPropertyList:dictionary
                                                               format:NSPropertyListBinaryFormat_v1_0
                                                              options:0
                                                                error:NULL];
    NSData *archived = [NSKeyedArchiver archivedDataWithRootObject:dictionary];`
    

    Size results for the different approaches smallest to largest

    • compactJson 46 bytes
    • prettyJson 57 bytes
    • plist 91 bytes
    • archived 316 bytes

提交回复
热议问题