Ios 更改字典';将所有值都转换为字符串

Ios 更改字典';将所有值都转换为字符串,ios,objective-c,casting,nsdictionary,nsmutabledictionary,Ios,Objective C,Casting,Nsdictionary,Nsmutabledictionary,我想把字典的all值改成String,怎么处理 例如: { @"a":"a", @"b":2, @"c":{ @"c1":3, @"c2":4 } } 我想转换成: { @"a":"a", @"b":"2", @"c":{ @"c1":"3", @"c2":"4" } } 怎么办?我整天都在想 如果我使用以下方法遍历字典值: NSArray *valueList = [dictio

我想把字典的all值改成String,怎么处理

例如:

{   @"a":"a", 
    @"b":2, 
    @"c":{
      @"c1":3,
      @"c2":4
    }
}
我想转换成:

{   @"a":"a", 
    @"b":"2", 
    @"c":{
      @"c1":"3",
      @"c2":"4"
    }
}
怎么办?我整天都在想

如果我使用以下方法遍历字典值:

NSArray *valueList = [dictionary allValues];

for (NSString * value in valueList) {
    // change the value to String
}

如果这个值是一本字典,那它呢


所以,有人可以帮你吗?

你可以为字典创建一个类别,并添加一些方法,比如
stringValueForKey:
。 实现可以是这样的:

- (NSString)stringValueForKey:(NSString*)key
{
   id value = self[key];
   if( [value respondsToSelector:@selector(stringValue)])
       return [value performSelector:@selector(stringValue)]
   return nil;
}

您可以为字典创建类别并添加一些方法,如
stringValueForKey:
。 实现可以是这样的:

- (NSString)stringValueForKey:(NSString*)key
{
   id value = self[key];
   if( [value respondsToSelector:@selector(stringValue)])
       return [value performSelector:@selector(stringValue)]
   return nil;
}

您可以使用递归方法来实现这一点,它将所有的
NSNumber
值更改为
NSString
,并调用自己的嵌套字典。由于字典在枚举时不能进行变异,因此将创建并填充一个新字典:

- (void)changeValuesOf:(NSDictionary *)dictionary result:(NSMutableDictionary *)result
{
    for (NSString *key in dictionary) {
        id value = dictionary[key];
        if ([value isKindOfClass: [NSDictionary class]]) {
            NSMutableDictionary * subDict = [NSMutableDictionary dictionary];
            result[key] = subDict;
            [self changeValuesOf:value result:subDict];
        } else if ([value isKindOfClass: [NSNumber class]]) {
            result[key] = [NSString stringWithFormat:@"%@", value];
        } else {
            result[key] = value;
        }
    }
}

NSDictionary *dictionary = @{@"a": @"a", @ "b":@2, @"c": @{@"c1": @3,  @"c2":@4 }};
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[self changeValuesOf:dictionary result:result];
NSLog(@"%@", result);

您可以使用递归方法来实现这一点,它将所有的
NSNumber
值更改为
NSString
,并调用自己的嵌套字典。由于字典在枚举时不能进行变异,因此将创建并填充一个新字典:

- (void)changeValuesOf:(NSDictionary *)dictionary result:(NSMutableDictionary *)result
{
    for (NSString *key in dictionary) {
        id value = dictionary[key];
        if ([value isKindOfClass: [NSDictionary class]]) {
            NSMutableDictionary * subDict = [NSMutableDictionary dictionary];
            result[key] = subDict;
            [self changeValuesOf:value result:subDict];
        } else if ([value isKindOfClass: [NSNumber class]]) {
            result[key] = [NSString stringWithFormat:@"%@", value];
        } else {
            result[key] = value;
        }
    }
}

NSDictionary *dictionary = @{@"a": @"a", @ "b":@2, @"c": @{@"c1": @3,  @"c2":@4 }};
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[self changeValuesOf:dictionary result:result];
NSLog(@"%@", result);

如果值是dictionary,那么如何?如果值是dictionary,则此方法将返回nil。为什么需要更改字符串的所有值?我不需要。但是op就是这么问的。如果值是dictionary,那么它呢?如果值是dictionary,这个方法将返回nil。为什么需要更改字符串的所有值?我不需要。但这正是op所问的。回答不错,虽然不考虑NSArray类,但这已经让我收到了指示。很抱歉,您的问题根本没有提到
NSArray
(除了
valueList
的类型)以及如何处理。请提出更具体的问题。回答得好,虽然不考虑NSArray类,但这已经让我收到了指示。很抱歉,您的问题根本没有提到
NSArray
(除了
valueList
的类型)以及如何处理。请问更具体的问题。