123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- //
- // NSDictionary+YMExtension.m
- // MSYOUPAI
- //
- // Created by YoMi on 2023/11/7.
- //
- #import "NSDictionary+YMExtension.h"
- @implementation NSDictionary (YMExtension)
- - (BOOL)boolValueForKey:(NSString *)key defaultValue:(BOOL)defaultValue {
- id tmpValue = [self objectForKey:key];
-
- if (tmpValue == nil || tmpValue == [NSNull null]) {
- return defaultValue;
- }
-
- if ([tmpValue isKindOfClass:[NSNumber class]]) {
- return [tmpValue boolValue];
- } else {
- @try {
- return [tmpValue boolValue];
- }
- @catch (NSException *exception) {
- return defaultValue;
- }
- }
- }
- - (NSInteger)integerValueForKey:(NSString *)key defaultValue:(NSInteger)defaultValue
- {
- id tmpValue = [self objectForKey:key];
-
- if (tmpValue == nil || tmpValue == [NSNull null])
- {
- return defaultValue;
- }
-
- if ([tmpValue isKindOfClass:[NSNumber class]])
- {
- return [tmpValue integerValue];
- }
- else
- {
- @try
- {
- return [tmpValue integerValue];
- }
- @catch (NSException *exception)
- {
- return defaultValue;
- }
- }
- }
- - (CGFloat)floatValueForKey:(NSString *)key defaultValue:(CGFloat)defaultValue {
- id tmpValue = [self objectForKey:key];
-
- if (tmpValue == nil || tmpValue == [NSNull null]) {
- return defaultValue;
- }
-
- if ([tmpValue isKindOfClass:[NSNumber class]]) {
- return [tmpValue floatValue];
- } else {
- @try {
- return [tmpValue floatValue];
- }
- @catch (NSException *exception) {
- return defaultValue;
- }
- }
- }
- - (time_t)timeValueForKey:(NSString *)key defaultValue:(time_t)defaultValue {
- NSString *stringTime = [self objectForKey:key];
- if ((id)stringTime == [NSNull null]) {
- stringTime = @"";
- }
- struct tm created;
- time_t now;
- time(&now);
-
- if (stringTime) {
- if (strptime([stringTime UTF8String], "%a %b %d %H:%M:%S %z %Y", &created) == NULL) {
- strptime([stringTime UTF8String], "%a, %d %b %Y %H:%M:%S %z", &created);
- }
- return mktime(&created);
- }
- return defaultValue;
- }
- - (long long)longLongValueValueForKey:(NSString *)key defaultValue:(long long)defaultValue {
- id tmpValue = [self objectForKey:key];
-
- if (tmpValue == nil || tmpValue == [NSNull null]) {
- return defaultValue;
- }
-
- if ([tmpValue isKindOfClass:[NSNumber class]]) {
- return [tmpValue longLongValue];
- } else {
- @try {
- return [tmpValue longLongValue];
- }
- @catch (NSException *exception) {
- return defaultValue;
- }
- }
- }
- - (NSString *)stringValueForKey:(NSString *)key defaultValue:(NSString *)defaultValue
- {
- id tmpValue = [self objectForKey:key];
-
- if (tmpValue == nil || tmpValue == [NSNull null])
- {
- return defaultValue;
- }
-
- if ([tmpValue isKindOfClass:[NSString class]])
- {
- return [NSString stringWithString:tmpValue];
- }
- else
- {
- @try
- {
- return [NSString stringWithFormat:@"%@",tmpValue];
- }
- @catch (NSException *exception)
- {
- return defaultValue;
- }
- }
- }
- - (NSArray *)arrayValueForKey:(NSString *)key defaultValue:(NSArray *)defaultValue
- {
- id tmpValue = [self objectForKey:key];
-
- if (tmpValue == nil || tmpValue == [NSNull null])
- {
- return defaultValue;
- }
-
- if ([tmpValue isKindOfClass:[NSArray class]])
- {
- return tmpValue;
- }
- else
- {
- return defaultValue;
- }
- }
- - (NSDictionary *)dictionaryValueForKey:(NSString *)key defaultValue:(NSDictionary *)defaultValue
- {
- id tmpValue = [self objectForKey:key];
-
- if (tmpValue == nil || tmpValue == [NSNull null])
- {
- return defaultValue;
- }
-
- if ([tmpValue isKindOfClass:[NSDictionary class]])
- {
- return tmpValue;
- }
- else
- {
- return defaultValue;
- }
- }
- - (NSString *)welfareCenterStringValueForKey:(NSString *)key defaultValue:(NSString *)defaultValue
- {
- id tmpValue = [self objectForKey:key];
-
- if (tmpValue == nil || tmpValue == [NSNull null])
- {
- return defaultValue;
- }
-
- if ([tmpValue isKindOfClass:[NSString class]])
- {
- if ([tmpValue isEqualToString:@""]) {
- return @"-";
- }else {
- return [NSString stringWithString:tmpValue];
- }
-
- }
- else
- {
- @try
- {
- NSString *welfareCenterString = [NSString stringWithFormat:@"%@",tmpValue];
- if ([welfareCenterString isEqualToString:@""]) {
- return @"-";
- }else {
- return welfareCenterString;
- }
-
- }
- @catch (NSException *exception)
- {
- return defaultValue;
- }
- }
- }
- @end
|