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];
仅做记录!