iOS NSDictionary 常用 Category(类目)分享

西门桃桃 2020-07-30 PM 1886℃ 0条

1、NSDictionary转json字符串:

- (NSString *)toJsonString {
    NSString *jsonString = nil;
    NSError *error;
#ifdef DEBUG
    NSJSONWritingOptions options = NSJSONWritingPrettyPrinted;
#else
    NSJSONWritingOptions options = 0;
#endif
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:options
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

- (NSString *)toMutableJsonString {
    NSMutableString *jsonString = nil;
    NSError *error;
#ifdef DEBUG
    NSJSONWritingOptions options = NSJSONWritingPrettyPrinted;
#else
    NSJSONWritingOptions options = 0;
#endif
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:options
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSMutableString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&error];
    if(error) {
        NSLog(@"Got an error: %@", error);
        return nil;
    }
    return dic;
}

2、NSDictionary中对象类型转为基本数据类型:

#pragma mark -NSDictionary
- (unsigned long long)unsignedLongLongForKey:(NSString *)key
{
    NSNumber* number = [self objectForKey:key];
    
    if (number != nil && [number isKindOfClass:[NSNumber class]])
        return [number unsignedLongLongValue];
    
    if (number != nil && [number isKindOfClass:[NSString class]])
        return [number longLongValue];
    
    return UINT64_MAX;
}

- (unsigned long)unsignedLongForKey:(NSString *)key
{
    NSNumber* number = [self objectForKey:key];
    
    if (number != nil && [number isKindOfClass:[NSNumber class]])
        return [number unsignedLongValue];
    
    if (number != nil && [number isKindOfClass:[NSString class]])
        return [number intValue];
    
    return UINT32_MAX;
}

- (unsigned int)unsignedIntForKey:(NSString *)key
{
    NSNumber* number = [self objectForKey:key];
    
    if (number != nil && [number isKindOfClass:[NSNumber class]])
        return [number unsignedIntValue];
    
    if (number != nil && [number isKindOfClass:[NSString class]])
        return [number intValue];
    
    return UINT32_MAX;
}

- (unsigned short)unsignedShortForKey:(NSString *)key
{
    NSNumber* number = [self objectForKey:key];
    
    if (number != nil && [number isKindOfClass:[NSNumber class]])
        return [number unsignedShortValue];
    
    if (number != nil && [number isKindOfClass:[NSString class]])
        return [number intValue];
    
    return UINT16_MAX;
}

- (int)intForKey:(NSString *)key
{
    NSNumber* number = [self objectForKey:key];
    
    if (number != nil && [number isKindOfClass:[NSNumber class]])
        return [number intValue];
    
    if (number != nil && [number isKindOfClass:[NSString class]])
        return [number intValue];
    
    return INT32_MAX;
}

- (BOOL)boolForKey:(NSString *)key
{
    NSNumber* number = [self objectForKey:key];
    
    if (number != nil && [number isKindOfClass:[NSNumber class]])
        return [number boolValue];
    
    if (number != nil && [number isKindOfClass:[NSString class]])
        return [number boolValue];
    
    return NO;
}

- (unsigned char)unsignedCharForKey:(NSString *)key
{
    NSNumber* number = [self objectForKey:key];
    
    if (number != nil && [number isKindOfClass:[NSNumber class]])
        return [number unsignedCharValue];
    
    if (number != nil && [number isKindOfClass:[NSString class]])
        return [number intValue];
    
    return UINT8_MAX;
}

- (char)charForKey:(NSString *)key
{
    NSNumber* number = [self objectForKey:key];
    
    if (number != nil && [number isKindOfClass:[NSNumber class]])
        return [number charValue];
    
    if (number != nil && [number isKindOfClass:[NSString class]])
        return [number intValue];
    
    return INT8_MAX;
}

- (float)floatForKey:(NSString *)key
{
    NSNumber* number = [self objectForKey:key];
    if (number != nil && [number isKindOfClass:[NSNumber class]])
        return [number floatValue];
    
    if (number != nil && [number isKindOfClass:[NSString class]])
        return [number floatValue];
    
    return 0.0f;
}

- (NSDate*)dateForKey:(NSString*)key
{
    NSNumber* number = [self objectForKey:key];
    
    if (number != nil && [number isKindOfClass:[NSNumber class]])
        return [NSDate dateWithTimeIntervalSince1970:[number unsignedIntValue]];
    
    if (number != nil && [number isKindOfClass:[NSString class]])
        return [NSDate dateWithTimeIntervalSince1970:[number integerValue]];
    
    return nil;
}

#pragma mark -NSMutableDictionary
- (void)setUnsignedLongLong:(unsigned long long)value forKey:(id)aKey
{
    NSNumber* number = [NSNumber numberWithUnsignedLongLong:value];
    [self setValue:number forKey:aKey];
}

- (void)setUnsignedLong:(unsigned long)value forKey:(id)aKey
{
    NSNumber* number = [NSNumber numberWithUnsignedLong:value];
    [self setValue:number forKey:aKey];
}

- (void)setUnsignedInt:(unsigned int)value forKey:(id)aKey
{
    NSNumber* number = [NSNumber numberWithUnsignedInt:value];
    [self setValue:number forKey:aKey];
}

- (void)setInt:(int)value forKey:(id)aKey
{
    NSNumber* number = [NSNumber numberWithInt:value];
    [self setValue:number forKey:aKey];
}

- (void)setBool:(BOOL)value forKey:(id)aKey
{
    NSNumber* number = [NSNumber numberWithBool:value];
    [self setValue:number forKey:aKey];
}

- (void)setUnsignedShort:(unsigned short)value forKey:(id)aKey
{
    NSNumber* number = [NSNumber numberWithUnsignedShort:value];
    [self setValue:number forKey:aKey];
}

- (void)setDateWithTimeIntervalSince1970:(unsigned long)value forKey:(id)aKey
{
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:value];
    [self setValue:date forKey:aKey];
}

- (void)setUnsignedChar:(unsigned char)value forKey:(id)aKey
{
    NSNumber* number = [NSNumber numberWithUnsignedChar:value];
    [self setValue:number forKey:aKey];
}

- (void)setChar:(char)value forKey:(id)aKey
{
    NSNumber* number = [NSNumber numberWithChar:value];
    [self setValue:number forKey:aKey];
}

- (void)setFloat:(float)value forKey:(id)aKey
{
    NSNumber* number = [NSNumber numberWithFloat:value];
    [self setValue:number forKey:aKey];
}

- (void)setNSStringWithUTF8String:(const char*)str forKey:(id)aKey
{
    NSString* nsstr = [NSString stringWithUTF8String:str];
    if(nsstr)
        [self setValue:nsstr forKey:aKey];
}
标签: Category, NSDictionary

非特殊说明,本博所有文章均为博主原创。

评论啦~