NSDictionary+YMExtension.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // NSDictionary+YMExtension.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2023/11/7.
  6. //
  7. #import "NSDictionary+YMExtension.h"
  8. @implementation NSDictionary (YMExtension)
  9. - (BOOL)boolValueForKey:(NSString *)key defaultValue:(BOOL)defaultValue {
  10. id tmpValue = [self objectForKey:key];
  11. if (tmpValue == nil || tmpValue == [NSNull null]) {
  12. return defaultValue;
  13. }
  14. if ([tmpValue isKindOfClass:[NSNumber class]]) {
  15. return [tmpValue boolValue];
  16. } else {
  17. @try {
  18. return [tmpValue boolValue];
  19. }
  20. @catch (NSException *exception) {
  21. return defaultValue;
  22. }
  23. }
  24. }
  25. - (NSInteger)integerValueForKey:(NSString *)key defaultValue:(NSInteger)defaultValue
  26. {
  27. id tmpValue = [self objectForKey:key];
  28. if (tmpValue == nil || tmpValue == [NSNull null])
  29. {
  30. return defaultValue;
  31. }
  32. if ([tmpValue isKindOfClass:[NSNumber class]])
  33. {
  34. return [tmpValue integerValue];
  35. }
  36. else
  37. {
  38. @try
  39. {
  40. return [tmpValue integerValue];
  41. }
  42. @catch (NSException *exception)
  43. {
  44. return defaultValue;
  45. }
  46. }
  47. }
  48. - (CGFloat)floatValueForKey:(NSString *)key defaultValue:(CGFloat)defaultValue {
  49. id tmpValue = [self objectForKey:key];
  50. if (tmpValue == nil || tmpValue == [NSNull null]) {
  51. return defaultValue;
  52. }
  53. if ([tmpValue isKindOfClass:[NSNumber class]]) {
  54. return [tmpValue floatValue];
  55. } else {
  56. @try {
  57. return [tmpValue floatValue];
  58. }
  59. @catch (NSException *exception) {
  60. return defaultValue;
  61. }
  62. }
  63. }
  64. - (time_t)timeValueForKey:(NSString *)key defaultValue:(time_t)defaultValue {
  65. NSString *stringTime = [self objectForKey:key];
  66. if ((id)stringTime == [NSNull null]) {
  67. stringTime = @"";
  68. }
  69. struct tm created;
  70. time_t now;
  71. time(&now);
  72. if (stringTime) {
  73. if (strptime([stringTime UTF8String], "%a %b %d %H:%M:%S %z %Y", &created) == NULL) {
  74. strptime([stringTime UTF8String], "%a, %d %b %Y %H:%M:%S %z", &created);
  75. }
  76. return mktime(&created);
  77. }
  78. return defaultValue;
  79. }
  80. - (long long)longLongValueValueForKey:(NSString *)key defaultValue:(long long)defaultValue {
  81. id tmpValue = [self objectForKey:key];
  82. if (tmpValue == nil || tmpValue == [NSNull null]) {
  83. return defaultValue;
  84. }
  85. if ([tmpValue isKindOfClass:[NSNumber class]]) {
  86. return [tmpValue longLongValue];
  87. } else {
  88. @try {
  89. return [tmpValue longLongValue];
  90. }
  91. @catch (NSException *exception) {
  92. return defaultValue;
  93. }
  94. }
  95. }
  96. - (NSString *)stringValueForKey:(NSString *)key defaultValue:(NSString *)defaultValue
  97. {
  98. id tmpValue = [self objectForKey:key];
  99. if (tmpValue == nil || tmpValue == [NSNull null])
  100. {
  101. return defaultValue;
  102. }
  103. if ([tmpValue isKindOfClass:[NSString class]])
  104. {
  105. return [NSString stringWithString:tmpValue];
  106. }
  107. else
  108. {
  109. @try
  110. {
  111. return [NSString stringWithFormat:@"%@",tmpValue];
  112. }
  113. @catch (NSException *exception)
  114. {
  115. return defaultValue;
  116. }
  117. }
  118. }
  119. - (NSArray *)arrayValueForKey:(NSString *)key defaultValue:(NSArray *)defaultValue
  120. {
  121. id tmpValue = [self objectForKey:key];
  122. if (tmpValue == nil || tmpValue == [NSNull null])
  123. {
  124. return defaultValue;
  125. }
  126. if ([tmpValue isKindOfClass:[NSArray class]])
  127. {
  128. return tmpValue;
  129. }
  130. else
  131. {
  132. return defaultValue;
  133. }
  134. }
  135. - (NSDictionary *)dictionaryValueForKey:(NSString *)key defaultValue:(NSDictionary *)defaultValue
  136. {
  137. id tmpValue = [self objectForKey:key];
  138. if (tmpValue == nil || tmpValue == [NSNull null])
  139. {
  140. return defaultValue;
  141. }
  142. if ([tmpValue isKindOfClass:[NSDictionary class]])
  143. {
  144. return tmpValue;
  145. }
  146. else
  147. {
  148. return defaultValue;
  149. }
  150. }
  151. - (NSString *)welfareCenterStringValueForKey:(NSString *)key defaultValue:(NSString *)defaultValue
  152. {
  153. id tmpValue = [self objectForKey:key];
  154. if (tmpValue == nil || tmpValue == [NSNull null])
  155. {
  156. return defaultValue;
  157. }
  158. if ([tmpValue isKindOfClass:[NSString class]])
  159. {
  160. if ([tmpValue isEqualToString:@""]) {
  161. return @"-";
  162. }else {
  163. return [NSString stringWithString:tmpValue];
  164. }
  165. }
  166. else
  167. {
  168. @try
  169. {
  170. NSString *welfareCenterString = [NSString stringWithFormat:@"%@",tmpValue];
  171. if ([welfareCenterString isEqualToString:@""]) {
  172. return @"-";
  173. }else {
  174. return welfareCenterString;
  175. }
  176. }
  177. @catch (NSException *exception)
  178. {
  179. return defaultValue;
  180. }
  181. }
  182. }
  183. @end