iphone - Cocoa Touch : What to release, 什么时候发布?

标签 iphone cocoa cocoa-touch

我正在努力了解何时以及必须在 Cocoa Touch 中发布什么内容,因为它没有垃圾收集功能。

此代码块来自苹果 iPhone 示例 periodicElements,他们释放 anElement 和 rawElementArray,但不释放 thePath、firstLetter、existingArray 和 tempArray?

我本以为至少应该释放 tempArray 和existingArray。

有聪明人可以解释一下为什么吗?

谢谢:)

- (void)setupElementsArray {
NSDictionary *eachElement;

// create dictionaries that contain the arrays of element data indexed by
// name
self.elementsDictionary = [NSMutableDictionary dictionary];
// physical state
self.statesDictionary = [NSMutableDictionary dictionary];
// unique first characters (for the Name index table)
self.nameIndexesDictionary = [NSMutableDictionary dictionary];

// create empty array entries in the states Dictionary or each physical state
[statesDictionary setObject:[NSMutableArray array] forKey:@"Solid"];
[statesDictionary setObject:[NSMutableArray array] forKey:@"Liquid"];
[statesDictionary setObject:[NSMutableArray array] forKey:@"Gas"];
[statesDictionary setObject:[NSMutableArray array] forKey:@"Artificial"];

// read the element data from the plist
NSString *thePath = [[NSBundle mainBundle]  pathForResource:@"Elements" ofType:@"plist"];
NSArray *rawElementsArray = [[NSArray alloc] initWithContentsOfFile:thePath];

// iterate over the values in the raw elements dictionary
for (eachElement in rawElementsArray)
{
    // create an atomic element instance for each
    AtomicElement *anElement = [[AtomicElement alloc] initWithDictionary:eachElement];

    // store that item in the elements dictionary with the name as the key
    [elementsDictionary setObject:anElement forKey:anElement.name];

    // add that element to the appropriate array in the physical state dictionary 
    [[statesDictionary objectForKey:anElement.state] addObject:anElement];

    // get the element's initial letter
    NSString *firstLetter = [anElement.name substringToIndex:1];
    NSMutableArray *existingArray;

    // if an array already exists in the name index dictionary
    // simply add the element to it, otherwise create an array
    // and add it to the name index dictionary with the letter as the key
    if (existingArray = [nameIndexesDictionary valueForKey:firstLetter]) 
    {
    [existingArray addObject:anElement];
    } else {
        NSMutableArray *tempArray = [NSMutableArray array];
        [nameIndexesDictionary setObject:tempArray forKey:firstLetter];
        [tempArray addObject:anElement];
    }

    // release the element, it is held by the various collections
    [anElement release];

}
// release the raw element data
[rawElementsArray release];



// create the dictionary containing the possible element states
// and presort the states data
self.elementPhysicalStatesArray = [NSArray arrayWithObjects:@"Solid",@"Liquid",@"Gas",@"Artificial",nil];
[self presortElementsByPhysicalState];

// presort the dictionaries now
// this could be done the first time they are requested instead

[self presortElementInitialLetterIndexes];

self.elementsSortedByNumber = [self presortElementsByNumber];
self.elementsSortedBySymbol = [self presortElementsBySymbol];

}

最佳答案

它们通过向类发送 +alloc 来创建 rawElementsArray,因此该对象由上面示例中的代码拥有,必须释放。与 anElement 类似。请注意,thePathtempArray 不是通过发送 +alloc+new-copy< 创建的 消息,因此调用代码不负责这些对象的生命周期。请看一下 Cocoa 内存管理文章集:

http://iamleeg.blogspot.com/2008/12/cocoa-memory-management.html

关于iphone - Cocoa Touch : What to release, 什么时候发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/430839/

相关文章:

iphone - 使用大量 CALayer 对象时优化性能的技巧?

iphone - 如何创建图像叠加层并将其添加到 MKMapView?

iphone - 如何在 Xcode 子项目中使用 InterfaceBuilder 文件?

javascript - 是否可以在 iPhone 应用程序中通过 JavaScript 绘制图形?

ios - segue 如何创建目标 ViewController?

ios - UISaveVideoAtPathToSavedPhotosAlbum 输出

iphone - 为什么 UIWebView 本地文件会出现 NSException 错误?

objective-c - NSTextView 在文本之间插入图像

ios - 如何在 iOS 中交换两个 UIWebView

cocoa - 如何从 Mac (OS X) 上的沙盒应用程序运行 AppleScript