iOS — distinguish an NSDictionary from an NSMutableDictionary?

后端 未结 6 1124
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 14:34

I have the following code fragment:

NSString* value = @\"value\";
NSString* key = @\"key\";
NSMutableDictionary* foo = [NSMutableDictionary dictionary];
NSDictio         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 14:44

    If you really don't care if it's mutable, but want to mutate it anyway, you could try this:

    NSString* value = @"value";
    NSString* key = @"key";
    
    NSDictionary* bar = [NSDictionary dictionary];
    NSLog(@"bar desc: %@", [bar description]);
    NSMutableDictionary* temp = [bar mutableCopy];
    [temp setValue:value forKey:key];
    bar = temp;
    NSLog(@"bar desc: %@", [bar description]);
    

提交回复
热议问题