NSDictionary+FIRMessaging.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseMessaging/Sources/NSDictionary+FIRMessaging.h"
  17. @implementation NSDictionary (FIRMessaging)
  18. - (NSString *)fcm_string {
  19. NSMutableString *dictAsString = [NSMutableString string];
  20. NSString *separator = @"|";
  21. for (id key in self) {
  22. id value = self[key];
  23. if ([key isKindOfClass:[NSString class]] && [value isKindOfClass:[NSString class]]) {
  24. [dictAsString appendFormat:@"%@:%@%@", key, value, separator];
  25. }
  26. }
  27. // remove the last separator
  28. if ([dictAsString length]) {
  29. [dictAsString deleteCharactersInRange:NSMakeRange(dictAsString.length - 1, 1)];
  30. }
  31. return [dictAsString copy];
  32. }
  33. - (BOOL)fcm_hasNonStringKeysOrValues {
  34. for (id key in self) {
  35. id value = self[key];
  36. if (![key isKindOfClass:[NSString class]] || ![value isKindOfClass:[NSString class]]) {
  37. return YES;
  38. }
  39. }
  40. return NO;
  41. }
  42. - (NSDictionary *)fcm_trimNonStringValues {
  43. NSMutableDictionary *trimDictionary = [NSMutableDictionary dictionaryWithCapacity:self.count];
  44. for (id key in self) {
  45. id value = self[key];
  46. if ([key isKindOfClass:[NSString class]] && [value isKindOfClass:[NSString class]]) {
  47. trimDictionary[(NSString *)key] = value;
  48. }
  49. }
  50. return trimDictionary;
  51. }
  52. @end