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

Objective c 为NSDictionary中的所有现有键设置相同的值,objective-c,cocoa,nsmutabledictionary,kvc,Objective C,Cocoa,Nsmutabledictionary,Kvc,在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).strin

在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添加一个函数来帮助您:

@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方法(
setValuesForKeysWithDictionary
setValue: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

当您想要使用KVC方法时,这种方法是有意义的,因为在接受的答案中不可能使用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";

可能它们还没有这方面的功能。我会在几天内接受你的回答,以防有人找到我想要的方法:pIt似乎你误解了这个问题。在我的示例中(对于
NSDictionary
),我询问是否可以使用KVC为平面字典中的所有键设置一个值。比如,一个带有键和值的Dict:
[0:“0”,1:“1”,2:“2”,3:“3”]
,我想让它
[0:“相同”,1:“相同”,2:“相同”,3:“相同”]
。这对你有意义吗?@Eddie上面的代码就是这么做的。我添加了一个示例代码。在任何情况下,注释中NSDictionary的值都应该是问题中的类型
SomeClass
,而不是
NSString
。即使使用
NSMutableArray
,它也不适用于
NSString
。这是一个很好的解释。但是,不可能直接为键设置值,而是需要对象作为桥接,并为对象的属性设置值,对吗?比如,不可能像我之前的评论那样设置它,是吗?@Eddie不可能
[[[someDictionary allValues] mutableCopy] setValue:@"SameValue" forKeyPath:@"someProperty"];

NSLog(@"%@", someDictionary);
key1 = "someProperty=SameValue, anotherProperty=111";
key2 = "someProperty=SameValue, anotherProperty=222";
key3 = "someProperty=SameValue, anotherProperty=333";