2017-03-08 89 views
1

在KVC中,我通常使用setValues:forKeyPath:爲集合中的對象設置相同的值。例如:爲NSDictionary中的所有現有密鑰設置相同的值

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]; 
} 

是否有一個簡單的字典解決方案,如上例所示?

回答

1

我不確定是否有一個函數(我猜沒有),但是如果你需要多次這樣做,你可以添加一個函數到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]; 
+0

可能是他們不具備一個功能(沒有?)。我會在幾天後接受你的回答,以防某人找到了我要求的方式:p – Eddie

1

您可以使用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"; 
+0

看來你誤解了這個問題。在我的示例中(對於'NSDictionary'),我問是否可以通過使用KVC在平面字典中設置* VALUE *到* ALL KEYS *。就像,一個帶鍵和值的字典:'[0:「0」,1:「1」,2:「2」,3:「3」]',我想使它成爲'[0: 「,1:」相同「,2:」相同「,3:」相同「]。你能理解這個嗎? – Eddie

+1

@Eddie這正是上面的代碼所做的。我已經添加了一個示例代碼。在任何情況下,你的評論中的NSDictionary的值應該是你的問題中的SomeClass類型,而不是'NSString'。即使使用「NSMutableArray」,它也不能用於'NSString'。 – mrvincenzo

+0

這是一個很好的解釋。但是,直接爲鍵設置值是不可能的,但需要將對象作爲橋接並將對象屬性的值設置爲對,我是否正確?就像我在之前的評論中不可能設置它,是嗎? – Eddie

相關問題