为 NSDictionary 中的所有现有键设置相同的值

Set the same value for all existing keys in an NSDictionary

在KVC中,我通常使用setValues:forKeyPath:来为集合中的对象设置相同的值。例如:

NSMutableArray <SomeClass *> *arr = [[NSMutableArray alloc] initWithCapacity:10];
for (int i = 0; i < 10; i++) {
    SomeClass *obj = [[SomeClass alloc] init];
    obj.stringProp = @(i).stringValue;
    [arr addObject:obj];
}

NSLog(@"- %@", [arr valueForKeyPath:@"stringProp"]);
[arr setValuesForKeysWithDictionary:@{@"stringProp" : @"Same same!"}];

NSLog(@"- %@", [arr valueForKeyPath:@"stringProp"]);
[arr setValue:@"Another" forKeyPath:@"stringProp"];

NSLog(@"- %@", [arr valueForKeyPath:@"stringProp"])

但是,对于字典,我们也可以使用它吗?现在我必须手动设置值:

NSMutableDictionary *myDict;
//... setupValues
NSString *valueToSet = @"Hehe";
for (NSString *key in myDict) {
    [myDict setObject:valueToSet forKey:key];
}

是否有字典的简单解决方案,如上面的示例?

我不确定是否有这个功能(我猜没有),但如果你需要多次这样做,你可以向 NSMutableDictionary 添加一个功能来帮助你:

@interface NSMutableDictionary (NSMutableDictionarySetAllKeysObject)
-(void)setAllKeysObject:(nonnull id)object;
@end

@implementation NSMutableDictionary (NSMutableDictionarySetAllKeysObject)
-(void)setAllKeysObject:(nonnull id)object
{
    for (NSString *key in self.allKeys) {
        [self setObject:object forKey:key];
    }
}
@end

所以你可以这样做:

NSString *valueToSet = @"Hehe";
[myDict setAllKeysObject:valueToSet];

您可以使用 allValues 方法首先获取所有值作为 NSArray。然后获取数组的可变副本。最后,使用您在可变数组的问题中提到的任何 KVC 方法(setValuesForKeysWithDictionarysetValue:forKeyPath:,等等...)


基于你的例子

[[[myDict allValues] mutableCopy] setValuesForKeysWithDictionary:@{@"stringProp" : @"Same same!"}];`

[[[myDict allValues] mutableCopy] setValue:@"Another" forKeyPath:@"stringProp"];`

或者实现一个类别

@interface NSMutableDictionary (KVCForValues)

- (void)setValuesForValuesWithKeysWithDictionary:(NSDictionary<NSString *, id> *)keyedValues;
- (void)setValue:(nullable id)value forValuesWithKeyPath:(NSString *)keyPath;

@end

@implementation NSMutableDictionary (KVCForValues)

- (void)setValuesForValuesWithKeysWithDictionary:(NSDictionary<NSString *,id> *)keyedValues {
  [[[self allValues] mutableCopy] setValuesForKeysWithDictionary:keyedValues];
}

- (void)setValue:(id)value forValuesWithKeyPath:(NSString *)keyPath {
  [[[self allValues] mutableCopy] setValue:value forKeyPath:keyPath];
}

@end

并加以利用

[myDict setValuesForValuesWithKeysWithDictionary:@{@"stringProp" : @"Same same!"}];

[myDict setValue:@"Another" forValuesWithKeyPath:@"stringProp"];

当您想使用已接受的答案无法实现的 KVC 方法时,这种方法很有意义


具体例子

假设SomeClass定义如下

@interface SomeClass: NSObject

@property (nonatomic, strong) NSString *someProperty;
@property (nonatomic, assign) NSInteger anotherProperty;

@end

@implementation SomeClass

- (NSString *)description {
    return [NSString stringWithFormat:@"someProperty=%@, anotherProperty=%d", self.someProperty, (int)self.anotherProperty];
}

@end

执行以下

SomeClass *value1 = [[SomeClass alloc] init];
value1.someProperty = @"aaa";
value1.anotherProperty = 111;

SomeClass *value2 = [[SomeClass alloc] init];
value2.someProperty = @"bbb";
value2.anotherProperty = 222;

SomeClass *value3 = [[SomeClass alloc] init];
value3.someProperty = @"ccc";
value3.anotherProperty = 333;

NSDictionary *someDictionary = @{@"key1": value1, @"key2": value2, @"key3": value3};

NSLog(@"%@", someDictionary);

将产生以下输出

key1 = "someProperty=aaa, anotherProperty=111";
key2 = "someProperty=bbb, anotherProperty=222";
key3 = "someProperty=ccc, anotherProperty=333";

执行后

[[[someDictionary allValues] mutableCopy] setValue:@"SameValue" forKeyPath:@"someProperty"];

NSLog(@"%@", someDictionary);

输出将是

key1 = "someProperty=SameValue, anotherProperty=111";
key2 = "someProperty=SameValue, anotherProperty=222";
key3 = "someProperty=SameValue, anotherProperty=333";