Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

NSDictionary+CordovaPreferences.m 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import "NSDictionary+CordovaPreferences.h"
  18. #import <Foundation/Foundation.h>
  19. @implementation NSDictionary (CordovaPreferences)
  20. - (id)cordovaSettingForKey:(NSString*)key
  21. {
  22. return [self objectForKey:[key lowercaseString]];
  23. }
  24. - (BOOL)cordovaBoolSettingForKey:(NSString*)key defaultValue:(BOOL)defaultValue
  25. {
  26. BOOL value = defaultValue;
  27. id prefObj = [self cordovaSettingForKey:key];
  28. if (prefObj == nil) {
  29. NSLog(@"The preference key \"%@\" is not defined and will default to \"%@\"",
  30. key,
  31. (defaultValue ? @"TRUE" : @"FALSE"));
  32. return value;
  33. }
  34. if ([prefObj isKindOfClass:NSString.class]) {
  35. prefObj = [prefObj lowercaseString];
  36. if (
  37. // True Case
  38. [prefObj isEqualToString:@"true"] ||
  39. [prefObj isEqualToString:@"1"] ||
  40. // False Case
  41. [prefObj isEqualToString:@"false"] ||
  42. [prefObj isEqualToString:@"0"]
  43. )
  44. {
  45. value = [prefObj isEqualToString:@"true"] || [prefObj isEqualToString:@"1"];
  46. }
  47. } else if (
  48. [prefObj isKindOfClass:NSNumber.class] &&
  49. (
  50. [prefObj isEqual: @YES] ||
  51. [prefObj isEqual: @NO]
  52. )
  53. )
  54. {
  55. value = [prefObj isEqual: @YES];
  56. }
  57. return value;
  58. }
  59. - (CGFloat)cordovaFloatSettingForKey:(NSString*)key defaultValue:(CGFloat)defaultValue
  60. {
  61. CGFloat value = defaultValue;
  62. id prefObj = [self cordovaSettingForKey:key];
  63. if (prefObj != nil) {
  64. value = [prefObj floatValue];
  65. }
  66. return value;
  67. }
  68. @end
  69. @implementation NSMutableDictionary (CordovaPreferences)
  70. - (void)setCordovaSetting:(id)value forKey:(NSString*)key
  71. {
  72. [self setObject:value forKey:[key lowercaseString]];
  73. }
  74. @end