iOS开发之Dictionary与NSData互转
作者:互联网
1、将NSData转换成Dictionary
/** 将二进制数据转换成字典*/
+ (NSDictionary *)dictionaryForJsonData:(NSData *)jsonData
{
if (![jsonData isKindOfClass:[NSData class]] || jsonData.length < 1) {
return nil;
}
id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
if (![jsonObj isKindOfClass:[NSDictionary class]]) {
return nil;
}
return [NSDictionary dictionaryWithDictionary:(NSDictionary *)jsonObj];
}
2、将Dictionary转为NSData
/** 将字典转换成json格式字符串,不含\n这些符号*/
+ (NSData *)compactFormatDataForDictionary:(NSDictionary *)dicJson
{
if (![dicJson isKindOfClass:[NSDictionary class]]) {
return nil;
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil];
if (![jsonData isKindOfClass:[NSData class]]) {
return nil;
}
return jsonData;
}
标签:return,nil,Dictionary,iOS,NSDictionary,jsonData,NSData,互转,class 来源: https://www.cnblogs.com/hecanlin/p/10752818.html