NSDictionary-Additions.m 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // NSDictionary-Additions.m
  3. // JasonDevelop
  4. //
  5. // Created by Jason Lee on 10/29/14.
  6. // Copyright (c) jasondevelop. All rights reserved.
  7. //
  8. #import "NSDictionary-Additions.h"
  9. @implementation NSDictionary (Additions)
  10. - (BOOL)hasKey:(NSString *)key {
  11. BOOL hasKey = NO;
  12. for (NSString *candidateKey in self.allKeys) {
  13. if ([key isEqualToString:candidateKey]) {
  14. hasKey = YES;
  15. break;
  16. }
  17. }
  18. return hasKey;
  19. }
  20. - (NSString *)jsonStringWithPrettyPrint:(BOOL) prettyPrint {
  21. NSError *error;
  22. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
  23. options:(NSJSONWritingOptions) (prettyPrint ? NSJSONWritingPrettyPrinted : 0)
  24. error:&error];
  25. if (!jsonData) {
  26. NSLog(@"bv_jsonStringWithPrettyPrint: error: %@", error.localizedDescription);
  27. return @"{}";
  28. } else {
  29. return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  30. }
  31. }
  32. @end