简体   繁体   English

在 iOS 和 Objective-C 的 NSMutableDictionary 中添加值

[英]Add values in NSMutableDictionary in iOS with Objective-C

I'm starting objective-c development 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 TDictionary and I use it like this:在 Delphi 中有 class 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 ?我该如何在objective-c中做到这一点? Is there equivalent functions to the above mentioned AddOrSetValue() or TryGetValue() ?是否有与上述AddOrSetValue() or TryGetValue()等效的功能?

Thank you.谢谢你。

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 NSNumber object, and make sure you call [myDictionary release] when you've finished with the dictionary). (注意:您不能将整数或其他原语直接存储在NSMutableDictionary中,因此需要将它们包装在NSNumber object 中,并确保在完成字典后调用[myDictionary release] )。

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)。

Yep:是的:

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

setObject:forKey: overwrites any existing object with the same key; setObject:forKey:用相同的密钥覆盖任何现有的 object; objectForKey: returns nil if the object doesn't exist. objectForKey:如果 object 不存在,则返回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:重新编辑:对于特定示例,存在更紧凑的方法:

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

Crazy indentation for readability.疯狂的缩进以提高可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM