NSNULL+Filtration.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // JSONKit+NSNULL.m
  3. // InAppPurchase-Mac
  4. //
  5. // Created by zhudongyong on 14-7-11.
  6. // Copyright (c) 2014年 kdanmobile. All rights reserved.
  7. //
  8. #import "NSNULL+Filtration.h"
  9. @implementation NSObject (NSNULL)
  10. - (BOOL)isNSNULL {
  11. return [self isKindOfClass:[NSNull class]];
  12. }
  13. - (id)filterNullObject {
  14. if ([self isKindOfClass:[NSArray class]]) {
  15. NSMutableArray *array = [NSMutableArray array];
  16. for (id item in (NSArray*)self) {
  17. if (![item isNSNULL]) {
  18. if ([item respondsToSelector:@selector(filterNullObject)]) {
  19. [array addObject:[item filterNullObject]];
  20. }else {
  21. [array addObject:item];
  22. }
  23. }
  24. }
  25. return array;
  26. }else if ([self isKindOfClass:[NSDictionary class]]) {
  27. NSMutableDictionary *dicInfo = [NSMutableDictionary dictionary];
  28. id value = nil;
  29. for (NSString *key in [(NSDictionary*)self allKeys]) {
  30. if (![key isNSNULL]) {
  31. value = ((NSDictionary*)self)[key];
  32. if (![value isNSNULL]) {
  33. if ([value respondsToSelector:@selector(filterNullObject)]) {
  34. [dicInfo setValue:[value filterNullObject]
  35. forKey:key];
  36. }else {
  37. [dicInfo setValue:value
  38. forKey:key];
  39. }
  40. }
  41. }
  42. }
  43. return dicInfo;
  44. }
  45. return self;
  46. }
  47. @end