我正在学习Stephen Kochan的“Objective-C编程”,我对NSMutableString *value1 = [[NSMutableString alloc ] initWithString: @"Value for Key one" ];
NSMutableString *value2 = [[NSMutableString alloc ] initWithString: @"Value for Key two" ];
NSMutableString *value3 = [[NSMutableString alloc ] initWithString: @"Value for Key three" ];
NSMutableString *value4 = [[NSMutableString alloc ] initWithString: @"Value for Key four" ];
NSString *key1 = @"key1";
NSString *key2 = @"key2";
NSString *key3 = @"key3";
NSString *key4 = @"key4";
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: value1, key1, value2, key2, value3, key3, nil];
NSDictionary *dictionaryCopy = [[NSDictionary alloc] init];
NSMutableDictionary *dictionaryMutableCopy = [[NSMutableDictionary alloc] init];
dictionaryCopy = [dictionary copy];
dictionaryMutableCopy = [dictionary mutableCopy];
[value1 setString: @"New value for Key one" ];
[value2 setString: @"New value for Key two" ];
[value3 setString: @"New value for Key three" ];
dictionaryMutableCopy[key4] = value4;
NSLog(@"All key for value 4");
for (NSValue *key in [dictionaryMutableCopy allKeysForObject:value4]) {
NSLog(@"key: %@", key);
}
NSLog(@"All values");
for (NSValue *val in [dictionaryMutableCopy allValues]) {
NSLog(@"value: %@", val);
}
for (NSValue *key in [dictionaryMutableCopy allKeys]) {
NSLog(@"Key: %@ value: %@", key, dictionary[key]);
}
的可变副本有疑问。
所以,这是我的代码:
NSMutableDictionary
您如何看待和结束我正在打印key 4
中的所有键/值,但对于key 4
我没有价值!
但是你怎么看到[Content of NSMutableDictionary][2]
的值是'null!
$scope.getClass = function(){
return $scope.class;
}
有什么问题?请帮忙
答案 0 :(得分:2)
在最后的for
循环中,您获得dictionary
而不是dictionaryMutableCopy
的值:
for (NSValue *key in [dictionaryMutableCopy allKeys]) {
NSLog(@"Key: %@ value: %@", key, dictionaryMutableCopy[key]);
// ^^^^^^^^^^^^^^^^^^^^^
}