来自NSMutableDictionary的键值不会打印

时间:2016-02-05 10:10:33

标签: objective-c xcode nsdictionary nsmutabledictionary nslog

我正在学习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我没有价值!

Screen from terminal

但是你怎么看到[Content of NSMutableDictionary][2] 的值是'null!

$scope.getClass = function(){
  return $scope.class;
}

有什么问题?请帮忙

1 个答案:

答案 0 :(得分:2)

在最后的for循环中,您获得dictionary而不是dictionaryMutableCopy的值:

for (NSValue *key in [dictionaryMutableCopy allKeys]) {
    NSLog(@"Key: %@ value: %@", key, dictionaryMutableCopy[key]);
    //                               ^^^^^^^^^^^^^^^^^^^^^
}