【问题标题】:how to remove the null value from NSMutablearray in objective c?如何从目标 c 中的 NSMutablearray 中删除空值?
【发布时间】:2018-09-06 19:46:43
【问题描述】:

由于我正在使用 json Web 服务并将值存储在 NSMutablearray 中,但我得到了 Null 响应。现在我想删除该 null 值并将其替换为空。谁能帮我解决这个错误。

(
        {
        Aadhar =1123323244;
        AddressProofCopy = "<null>";
        Advertisement = "<null>";
        AgreementCopy = "<null>";
        ApCity = ggg;
        ApDistrict = g;
        ApDoorNo = g;
        ApFGFirstName = v;
        ApFGSurName = g;
        ApGender = F;
        ApPIN = 12323;
        ApStreet = h;
        AppStatus = "<null>";
        ApplRefStageStatus = N;
        ApplicantFirstName = g;
        ApplicantPhoto = "<null>";
        ApplicantSurName = g;
        ApplicationDate = "2018-09-06T00:00:00.000Z";
        ApplicationType = New;
        BuldingType = "<null>";
        District = 132424;
        DistrictId = 11;
        DocVerifiedBy = "<null>";
        DocVerifiedDate = "<null>";
        DocVerifiedMessage = "<null>";
        DocVerifiedStatus = "<null>";
    }
)

【问题讨论】:

  • 请分享你的模型是什么样的。是字典数组吗?

标签: ios objective-c


【解决方案1】:

请验证此链接。https://github.com/bismasaeed00/NullReplacer 对我来说工作正常。

试试这个..

#import "NSArray+NullReplacement.h"
#import "NSDictionary+NullReplacement.h"

之后,使用以下行传递您的字典/数组 JSON 响应,

NSArray *newvalue = [yourarrayresponse arrayByReplacingNullsWithBlanks];

NSDictionary *newvalue = [yourdictionary dictionaryByReplacingNullsWithBlanks];

如果遇到任何错误,请添加

#import "NSArray+NullReplacement.h"

在 NSDictionary+NullReplacement 类和

#import "NSDictionary+NullReplacement.h" 

在 NSDictionary+NullReplacement 类文件中。

【讨论】:

    【解决方案2】:
    for(NSDictionary *dic in array)
    {
    for (NSString *key in dictionar.allKeys)
    {
        if ([[dictionary objectForKey:key] isKindOfClass:[NSNull class]])
        {
            [dictionary setObject:@"" forKey:key];
        }
    
    }
    }
    

    【讨论】:

      【解决方案3】:

      这不是空值,而是带有值/字符 "&lt;null&gt;" 的字符串。

      这是完整的示例工作代码。你可以使用这个:

      #import "ViewController.h"
      
      @interface ViewController ()
      
      @end
      
      @implementation ViewController
      
      - (void)viewDidLoad {
          [super viewDidLoad];
      
          NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                              @"1123323244",@"Aadhar",
                                              @"<null>",@"AddressProofCopy",
                                              nil];
          NSArray * array = [NSArray arrayWithObject:mutableDict];
      
          for(NSDictionary *dic in array)
              {
                  for (NSString *key in dic.allKeys)
                  {
                      if ([[dic valueForKey:key] isEqualToString:@"<null>"])
                      {
                          [mutableDict setObject:@"" forKey:key];
                      }
      
                  }
              }
      
          NSLog(@"Updated dict - %@", mutableDict);
      }
      
      @end
      

      输出:

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多