Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSMutableDictionary中的键的值不为';t打印_Objective C_Xcode_Nsdictionary_Nsmutabledictionary_Nslog - Fatal编程技术网

Objective c NSMutableDictionary中的键的值不为';t打印

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

我正在向Stephen Kochan学习“Objective-C编程”,我对
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
是正常的。我猜你是在学习教程,但请记住这一点。