简体   繁体   English

NSMutableDictionary上的setObject:forKey:是否分配?

[英]Does setObject:forKey: on NSMutableDictionary do an allocation?

Say I have the following: 说我有以下几点:

NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];

Now, when I do 现在,当我做

[dict setObject:object forKey:key];

Is a new NSMutableDictionary or NSDictionary allocated, and its values simply added to the NSMutableDictionary , sort of like doing a concat with a String in Java? 是否分配了新的NSMutableDictionaryNSDictionary ,并将其值简单地添加到NSMutableDictionary ,就像在Java中用String进行连接一样? Or is it that a new key value pair is simply inserted? 还是只是插入了一个新的键值对?

Basically, what I am asking is 基本上,我要问的是

Say I do this : 说我这样做:

@implementation ....

+(void) initialize 
{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
}

and then do : - (void) addValue { [dict setObject:object forKey:key]; 然后执行:-(void)addValue {[dict setObject:object forKey:key]; } }

Will that be worse or the same as doing : 是否会更糟或与这样做相同:

- (void) addValue
{
    NSDictionary dict = @[key:object]; 
}    
or 

+ (void) initialize
{
    NSDictionary * dict = @[key:object]; 
}

I do understand that +initialize is called only once. 我确实知道+initialize仅被调用一次。 What if, in addition, this class was to only be called only once? 此外,如果此类仅被调用一次怎么办?

As you say you come from Java, both languages have frameworks/libraries with immutable (constant) and mutable (modifiable) data structures so the concepts should be familiar to you. 正如您所说的,您来自Java,这两种语言都具有带有不变(恒定)和可变(可修改)数据结构的框架/库,因此您应该熟悉这些概念。 You mention dictionaries and strings, so roughly the correspondence is: 您提到了字典和字符串,所以大致的对应关系是:

  • Immutable strings: NSString (Cocoa), String (Java) 不可变的字符串: NSString (Cocoa), String (Java)
  • Mutable string: NSMutableString (Cocoa), StringBuffer (Java) 可变字符串: NSMutableString (Cocoa), StringBuffer (Java)
  • Mutable dictionary/map: NSMutableDictionary (Cocoa), HashMap (Java) 可变字典/地图: NSMutableDictionary (Cocoa), HashMap (Java)

HTH 高温超导

To answer your title question, no, setObject does not allocate an NSMutableDictionary. 要回答您的标题问题,不,setObject不分配NSMutableDictionary。 Same goes for NSMutableArray and addObject. 这同样适用于的NSMutableArray和ADDOBJECT。 To understand why, remember that Objective-C sets an object to nil when an object is defined without being allocated. 要理解为什么,请记住,Objective-C的设置对象为nil当对象没有被分配来定义。 Messages sent to nil objects result in nil, which is a very cool thing and can be used to great effect. 发送到零对象的消息导致零,这是一个非常酷的事情,可以用有很大的影响。

The other question I think you are asking is about when to allocate the NSMutableDictionary (or any object that requires allocation). 我想您要问的另一个问题是关于何时分配NSMutableDictionary(或任何需要分配的对象)。 The usual way is with lazy instantiation via custom getters. 通常的方法是通过自定义getter进行延迟实例化。 If you are absolutely sure that you'll be using the object it's ok to put it an init method (as in your last example). 如果您完全确定将使用该对象,则可以将其设置为init方法(如您上一个示例中所示)。

To see how to use lazy instantiation via a custom getter, create a single view application in Xcode (ie, File->New project->Single View Application, etc) open the ViewController.m file, and start with this property (in the interface): 要查看如何通过自定义getter使用惰性实例化,请在Xcode中创建一个单一视图应用程序(即File-> New project-> Single View Application等),然后打开ViewController.m文件,并以该属性开头(在接口):

@property (nonatomic, strong) NSMutableDictionary *mutableDictionary;

Here's your custom getter (in the implementation): 这是您的自定义获取器(在实现中):

- (NSMutableDictionary *)mutableDictionary
{
    if(!_mutableDictionary) {
        _mutableDictionary = [[NSMutableDictionary alloc] init];
    }
    return _mutableDictionary;
}

With that in place, the first time you have a 有了这个,您第一次有了

[mutableDictionary setObject:@"myValue" forKey:@"myKey"]

the getter function will run the if block to allocate it for you. getter函数将运行if块为您分配它。 Every subsequent setObject will simply return the existing mutable dictionary. 以后的每一次的setObject将简单地返回现有的可变字典。

No, adding objects to a dictionary does not cause memory allocation. 不,将对象添加到字典不会导致内存分配。 The dictionary keeps a strong reference to any objects you add, but those objects are not copied or re-allocated. 字典对添加的任何对象都有很强的引用,但是这些对象不会被复制或重新分配。

Allocating a mutable dictionary and then adding objects to it differs from creating a fixed non-mutable dictionary only in that you get a mutable dictionary. 分配可变字典然后向其添加对象与创建固定的非可变字典的区别仅在于,您获得了可变字典。 That might allocate a very tiny bit more memory (depending on whether NSMutableDictionary needs more memory than NSDictionary ). 那可能会分配很少的内存(取决于NSMutableDictionary是否需要比NSDictionary更多的内存)。 But unless you're really, really up against the wall, memory-wise, it's not worth considering. 但是除非您真的真的真的站在内存上,否则就不值得考虑。

Also note that while +initialize is only called once, it's a class method and not an instance method. 还要注意,虽然+initialize仅被调用一次,但这是一个方法,而不是实例方法。 If your dictionary is an instance property, you can't create it there. 如果字典是实例属性,则不能在其中创建它。

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

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