智莺生活APP的阿里百川 Flutter 插件
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

FlutterAlibcTools.m 2.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // FlutterAlibcTools.m
  3. // flutter_alibc
  4. //
  5. // Created by 吴兴 on 2019/8/29.
  6. //
  7. #import "FlutterAlibcTools.h"
  8. @implementation FlutterAlibcTools
  9. + (BOOL)isNil:(NSString *)string {
  10. if (string == nil) {
  11. return YES;
  12. }
  13. if ([string isKindOfClass:[NSNull class]]) {
  14. return YES;
  15. }
  16. return [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0;
  17. }
  18. + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
  19. if (jsonString == nil) {
  20. return nil;
  21. }
  22. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  23. NSError *err;
  24. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
  25. options:NSJSONReadingMutableContainers
  26. error:&err];
  27. if(err)
  28. {
  29. NSLog(@"json解析失败:%@",err);
  30. return nil;
  31. }
  32. return dic;
  33. }
  34. + (id)nullToNil:(id)obj{
  35. if ([obj isKindOfClass:[NSNull class]]) {
  36. return nil;
  37. }
  38. return obj;
  39. }
  40. #pragma mark - 公有方法,直接使用
  41. //类型识别:将所有的NSNull类型转化成@""
  42. +(id)changeType:(id)myObj
  43. {
  44. if ([myObj isKindOfClass:[NSDictionary class]])
  45. {
  46. return [self nullDic:myObj];
  47. }
  48. else if([myObj isKindOfClass:[NSArray class]])
  49. {
  50. return [self nullArr:myObj];
  51. }
  52. else if([myObj isKindOfClass:[NSString class]])
  53. {
  54. return [self stringToString:myObj];
  55. }
  56. else if([myObj isKindOfClass:[NSNull class]])
  57. {
  58. return [self nullToString];
  59. }
  60. else
  61. {
  62. return myObj;
  63. }
  64. }
  65. #pragma mark - 私有方法
  66. //将NSDictionary中的Null类型的项目转化成@""
  67. +(NSDictionary *)nullDic:(NSDictionary *)myDic
  68. {
  69. NSArray *keyArr = [myDic allKeys];
  70. NSMutableDictionary *resDic = [[NSMutableDictionary alloc]init];
  71. for (int i = 0; i < keyArr.count; i ++)
  72. {
  73. id obj = [myDic objectForKey:keyArr[i]];
  74. obj = [self changeType:obj];
  75. [resDic setObject:obj forKey:keyArr[i]];
  76. }
  77. return resDic;
  78. }
  79. //将NSArray中的Null类型的项目转化成@""
  80. +(NSArray *)nullArr:(NSArray *)myArr
  81. {
  82. NSMutableArray *resArr = [[NSMutableArray alloc] init];
  83. for (int i = 0; i < myArr.count; i ++)
  84. {
  85. id obj = myArr[i];
  86. obj = [self changeType:obj];
  87. [resArr addObject:obj];
  88. }
  89. return resArr;
  90. }
  91. //将NSString类型的原路返回
  92. +(NSString *)stringToString:(NSString *)string
  93. {
  94. return string;
  95. }
  96. //将Null类型的项目转化成@""
  97. +(NSString *)nullToString
  98. {
  99. // return @"<NSNull>";
  100. return @"";
  101. }
  102. @end