【问题标题】:Collections of collections and Objective-C memory management集合的集合和 Objective-C 内存管理
【发布时间】:2009-09-17 17:18:06
【问题描述】:

我有一个非常简单的问题。虽然我对 Objective-C 还很陌生,但我对语言和内存管理模型已经很熟悉了。

我非常了解所有权以及显式和隐式的概念。我还了解,当您向集合添加任何内容时,它会获取键和值的所有权,并在发布时释放该所有权。

我的问题与发布一系列收藏品(又名包)有关。我有以下代码:


    // levelDict is a member NSMutableDictionary on the Class

    BOOL newDict = NO;
    BOOL newArray = NO;

    NSNumber *levelKey = [NSNumber numberWithInt:toLevel];
    NSMutableDictionary *dict = [levelDict objectForKey:levelKey];
    if ( dict == nil ) {
        dict = [[NSMutableDictionary alloc] init];
        [levelDict setObject:dict forKey:levelKey];
        newDict = YES;
    }

    // Now look for the array...
    NSNumber *typeKey = [NSNumber numberWithInt:objectType];
    NSMutableArray *array = [dict objectForKey:typeKey];
    if ( array == nil ) {
        array = [[NSMutableArray alloc] init];
        [dict setObject:array forKey:typeKey];
        newArray = YES;
    }

    // Now add the object to the array...
    [array addObject:object];

    // Deal with our memory management
    if ( newArray ) {
        [array release];
    }

    if ( newDict ) {
        [dict release];
    }

此代码创建一个 Map,其中每个条目都包含一个 Array(又名 Bag)。如果我释放拥有每个条目的对象数组的字典对象 levelDict,我假设释放也会通过数组级联?还是我必须遍历字典并显式释放每个数组?

现在是额外的信用问题 - 我为什么要这样做而不是定义单个集合对象?好吧,在 Java 等其他语言中,对象实例化可能会变得非常昂贵。我假设这是 Objective-C 的情况。关联数组的映射非常有效。

谢谢 布莱恩

【问题讨论】:

    标签: iphone objective-c memory-management collections dictionary


    【解决方案1】:

    当一个字典被释放时,所有的键和值都会被释放。如果是数组,释放它们也会释放数组中的所有条目,以此类推。

    当然,向字典添加任何内容都会保留键和值,向可变数组添加任何内容都会保留条目。

    很简单...

    【讨论】:

    • 谢谢!我很确定是这样,但想要确认。
    【解决方案2】:

    来自集合编程指南:http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html

    “当你将一个对象添加到一个Objective-C数组时,这个对象不会被复制,而是在它的id被添加到数组之前接收一个retain消息。当一个数组被释放时,每个元素都会被发送一个发布消息。”

    字典和集合的行为方式相同。

    【讨论】:

      【解决方案3】:

      只要NSDictionary 是唯一保留对给定NSObject(包括NSArray)引用的容器,它就会在NSDictionary 这样做时被释放。

      【讨论】:

        【解决方案4】:

        使用自动释放的 dict/array 代码更简单,因为分支不会影响保留计数。

        // levelDict is a member NSMutableDictionary on the Class
        
        NSNumber *levelKey = [NSNumber numberWithInt:toLevel];
        NSMutableDictionary *dict = [levelDict objectForKey:levelKey];
        if ( dict == nil ) {
            dict = [NSMutableDictionary dictionary];
            [levelDict setObject:dict forKey:levelKey];
        }
        
        // Now look for the array...
        NSNumber *typeKey = [NSNumber numberWithInt:objectType];
        NSMutableArray *array = [dict objectForKey:typeKey];
        if ( array == nil ) {
            array = [NSMutableArray array];
            [dict setObject:array forKey:typeKey];
        }
        
        // Now add the object to the array...
        [array addObject:object];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-27
          相关资源
          最近更新 更多