1
我正在学习Stephen Kochan的“Objective-C编程”,我遇到了NSDictionary
的可变副本问题。 所以,这里是我的代码:来自NSMutableDictionary的键的值不打印
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]
什么问题?请帮助
哦,非常感谢!我知道这将是一个愚蠢的错误! –
@NikitaBonachev使用'NSValue'的BTW对我而言并不常见。我从来没有用过它;如果你知道键是字符串,那么使用'NSString'是正常的。我想你正在学习一个教程,但请牢记这一点。 – trojanfoe
好的,感谢您的解决方案和建议。 –