使用 Objective-C 在 iOS 中的 NSMutableDictionary 中添加值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6519957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:34:02  来源:igfitidea点击:

Add values in NSMutableDictionary in iOS with Objective-C

iosobjective-cnsmutabledictionary

提问by Miguel E

I'm starting objective-cdevelopment and I would like to ask the best way to implement a list of keys and values.

我正在开始objective-c开发,我想问一下实现键和值列表的最佳方法。

In Delphi there is the class TDictionaryand I use it like this:

在 Delphi 中有这个类TDictionary,我像这样使用它:

myDictionary : TDictionary<string, Integer>;

bool found = myDictionary.TryGetValue(myWord, currentValue);
if (found)
{
    myDictionary.AddOrSetValue(myWord, currentValue+1);
} 
else
{
    myDictionary.Add(myWord,1);
}

How can I do it in objective-c? Is there equivalent functions to the above mentioned AddOrSetValue() or TryGetValue()?

我怎样才能做到objective-c?是否有与上述等效的功能AddOrSetValue() or TryGetValue()

Thank you.

谢谢你。

回答by Tom Jefferys

You'd want to implement your example along these lines:

您希望按照以下方式实现您的示例:

EDIT:

编辑:

//NSMutableDictionary myDictionary = [[NSMutableDictionary alloc] init];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];

NSNumber *value = [myDictionary objectForKey:myWord];

if (value)
{
    NSNumber *nextValue = [NSNumber numberWithInt:[value intValue] + 1];
    [myDictionary setObject:nextValue  forKey:myWord];
} 
else
{
    [myDictionary setObject:[NSNumber numberWithInt:1] forKey:myWord]
}

(Note: you can't store ints or other primitives directly in a NSMutableDictionary, hence the need to wrap them in an NSNumberobject, and make sure you call [myDictionary release]when you've finished with the dictionary).

(注意:您不能将整数或其他原语直接存储在 a 中NSMutableDictionary,因此需要将它们包装在一个NSNumber对象中,并确保[myDictionary release]在完成字典后调用)。

回答by Jesse Rusak

The other answers are correct, but there is more modern syntax for this now. Rather than:

其他答案是正确的,但现在有更现代的语法。而不是:

[myDictionary setObject:nextValue  forKey:myWord];

You can simply say:

你可以简单地说:

myDictionary[myWord] = nextValue;

Similarly, to get a value, you can use myDictionary[key]to get the value (or nil).

同样,要获取一个值,您可以使用myDictionary[key]来获取该值(或 nil)。

回答by Williham Totland

Yep:

是的:

- (id)objectForKey:(id)key;
- (void)setObject:(id)object forKey:(id)key;

setObject:forKey:overwrites any existing object with the same key; objectForKey:returns nilif the object doesn't exist.

setObject:forKey:用相同的键覆盖任何现有对象;如果对象不存在则objectForKey:返回nil

Edit:

编辑:

Example:

例子:

- (void)doStuff {
  NSMutableDictionary *dict = [NSMutableDictionary dictionary];

  [dict setObject:@"Foo" forKey:@"Key_1"]; // adds @"Foo"
  [dict setObject:@"Bar" forKey:@"Key_2"]; // adds @"Bar"

  [dict setObject:@"Qux" forKey:@"Key_2"]; // overwrites @"Bar"!

  NSString *aString = [dict objectForKey:@"Key_1"]; // @"Foo"
  NSString *anotherString = [dict objectForKey:@"Key_2"]; // @"Qux"
  NSString *yas = [dict objectForKey:@"Key_3"]; // nil
}

Reedit:For the specific example there exists a more compact approach:

Reedit:对于特定示例,存在更紧凑的方法:

[dict
  setObject:
    [NSNumber numberWithInteger:([[dict objectForKey:@"key"] integerValue] + 1)]
  forKey:
    @"key"
 ];

Crazy indentation for readability.

疯狂的缩进以提高可读性。