Iphone 可可触控:发布什么,何时发布?

Iphone 可可触控:发布什么,何时发布?,iphone,cocoa,cocoa-touch,Iphone,Cocoa,Cocoa Touch,我正在努力理解在Cocoa Touch中我必须放松的时间和内容,因为它没有垃圾收集功能 此代码块来自苹果iphone示例PeriodiceElements,它们发布了一个元素和rawElementArray,但没有路径、第一个字母、现有数组和tempArray 我认为至少应该发布tempArray和existingArray 请聪明人给我解释一下原因好吗 谢谢: - (void)setupElementsArray { NSDictionary *eachElement; // create

我正在努力理解在Cocoa Touch中我必须放松的时间和内容,因为它没有垃圾收集功能

此代码块来自苹果iphone示例PeriodiceElements,它们发布了一个元素和rawElementArray,但没有路径、第一个字母、现有数组和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来创建rawlelementsarray,因此该对象属于上面示例中的代码,必须释放。与元素类似。请注意,路径和临时数组不是通过发送+alloc、+new或-copy消息创建的,因此调用代码不对这些对象的生存期负责。请看一下这组Cocoa内存管理文章:

它们通过向类发送+alloc来创建rawlelementsarray,因此该对象由上面示例中的代码所有,必须释放。与元素类似。请注意,路径和临时数组不是通过发送+alloc、+new或-copy消息创建的,因此调用代码不对这些对象的生存期负责。请看一下这组Cocoa内存管理文章:


您不必释放tempArray的原因是,它已被分配,然后立即自动释放。Autorelease是一种在将来某个时候安排释放调用的方法,这样API的调用方就不必显式地释放结果


提供了对Objective C内存管理策略的详细解释,并且解释得比我好得多。

您不必释放tempArray的原因是它已被分配,然后立即自动释放。Autorelease是一种在将来某个时候安排释放调用的方法,这样API的调用方就不必显式地释放结果


提供了对Objective C的内存管理策略的详细解释,并且解释得比我好得多。

惯例是,当您使用类方法创建对象时,它应该被自动删除。这意味着在运行循环结束时,当刷新自动释放池时,这些对象将被释放。但是,如果您使用与+alloc]-init]相同的+alloc]-init]或-copy、-mutableCopy或+new来创建某个内容,那么它将不会被自动释放

例如:

NSArray *array1 = [NSArray arrayWithObject:@"foo"];
NSArray *array2 = [[NSArray alloc] initWithObject:@"foo"];
Array1将自动释放,您无需担心。Array2需要手动释放。或者,您也可以这样做:

NSArray *array2 = [[[NSArray alloc] initWithObject:@"foo"] autorelease];
这与+arrayWithObject:的功能差不多


当然,这导致了对实例变量生命周期的重要考虑。如果使用array2创建实例变量,则可以,因为它的保留计数为1。但是,array1需要保留,否则它将在运行循环结束时自动删除,保留计数为0,因此它将被释放,留下一个悬空指针。

惯例是,当您使用类方法创建对象时,它应该被自动删除。这意味着在运行循环结束时,当刷新自动释放池时,这些对象将被释放。但是,如果您使用与+alloc]-init]相同的+alloc]-init]或-copy、-mutableCopy或+new来创建某个内容,那么它将不会被自动释放

例如:

NSArray *array1 = [NSArray arrayWithObject:@"foo"];
NSArray *array2 = [[NSArray alloc] initWithObject:@"foo"];
Array1将自动释放,您无需担心。Array2需要手动释放。或者,您也可以这样做:

NSArray *array2 = [[[NSArray alloc] initWithObject:@"foo"] autorelease];
这与+arrayWithObject:的功能差不多


当然,这导致了对实例变量生命周期的重要考虑。如果使用array2创建实例变量,则可以,因为它的保留计数为1。但是,需要保留数组1,否则它将在运行循环结束时自动删除,使其保留计数为0,因此它将被释放,您将得到一个悬空指针。

这与。。。你能解释一下吗?另外,您可以定义runloop吗?顺便说一句,将代码缩进4个空格以正确格式化。谢谢你的回答!这几乎就是。。。你能解释一下吗?另外,您可以定义runloop吗?顺便说一句,将代码缩进4个空格以正确格式化。谢谢你的回答!