在KVC中,我通常使用setValues:forKeyPath:
为集合中的对象设置相同的值。 E.g:
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];
}
是否有一个字典的简单解决方案,如上面的示例?
答案 0 :(得分:1)
我不确定是否有功能(我猜它没有),但是如果您需要多次执行此操作,您可以向NSMutableDictionary添加一个函数来帮助您:< / p>
@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];
答案 1 :(得分:1)
您可以先使用allValues
方法将所有值设为NSArray
。然后得到一个可变的数组副本。最后,在可变数组(setValuesForKeysWithDictionary
,setValue:forKeyPath:
等中使用您在问题中提到的任何KVC方法...)
基于您的示例
[[[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";