ios开发之--NSDictionary和NSData之间的互转/NSString和NSData之间的互转

NSDictionary转NSData,代码如下:

+(NSData*)returnDataWith:(NSDictionary*)dict
{
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; return data;
}

NSData转NSDictionary,代码如下:

+(NSDictionary*)returnDictionaryWithDataPath:(NSData*)data
{
NSString *receiveStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSData * datas = [receiveStr dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:datas options:NSJSONReadingMutableLeaves error:nil]; return jsonDict;
}

NSData转NSString,代码如下:

NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  

NSString转NSData,代码如下:

NSData *data = [@"test" dataUsingEncoding:NSUTF8StringEncoding];  

仅做记录!

上一篇:C++ 获取特定进程的CPU使用率<转>


下一篇:【Luogu1344】追查坏牛奶(最小割)