Objective c NSMutableDictionary中的键的值不为';t打印
我正在向Stephen Kochan学习“Objective-C编程”,我对Objective c NSMutableDictionary中的键的值不为';t打印,objective-c,xcode,nsdictionary,nsmutabledictionary,nslog,Objective C,Xcode,Nsdictionary,Nsmutabledictionary,Nslog,我正在向Stephen Kochan学习“Objective-C编程”,我对NSDictionary的可变副本有一个问题。 这是我的代码: NSMutableString *value1 = [[NSMutableString alloc ] initWithString: @"Value for Key one" ]; NSMutableString *value2 = [[NSMutableString alloc ] initWithString: @"Value for Key
NSDictionary
的可变副本有一个问题。
这是我的代码:
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
中的所有键/值,但是对于键4
我没有值
但是如何查看键4的值不是空的
[Content of NSMutableDictionary][2]
有什么问题吗?请帮助对于循环,在最后的
中,您将从字典
而不是字典mutablecopy
获取值:
for (NSValue *key in [dictionaryMutableCopy allKeys]) {
NSLog(@"Key: %@ value: %@", key, dictionaryMutableCopy[key]);
// ^^^^^^^^^^^^^^^^^^^^^
}
哦,非常感谢!我知道这将是一个愚蠢的错误@尼基塔博纳切夫顺便说一句,使用NSValue
对我来说是不寻常的。我从来没有用过它;如果您知道键是字符串,则使用NSString
是正常的。我猜你是在学习教程,但请记住这一点。