What increases an object's retain count?

前端 未结 3 473
面向向阳花
面向向阳花 2020-12-03 08:27

Here is code I am referring to.

// Person.h

@interface Person : NSObject {
    NSString *firstName;
    NSString *lastName;
}
@end

// Person.m

@implementa         


        
3条回答
  •  再見小時候
    2020-12-03 08:43

    You should generally /not/ be worried about the retain count. That's internally implemented. You should only care about whether you want to "own" an object by retaining it. In the code above, the array should own the object, not you (outside of the loop you don't even have reference to it except through the array). Because you own [[Person alloc] init], you then have to release it.

    Thus

    Person *p = [[Person alloc] init];
    [array addObject:p];
    [p release];
    

    Also, the caller of "getPeople" should not own the array. This is the convention. You should autorelease it first.

    NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
    

    You'll want to read Apple's documentation on memory management: http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

提交回复
热议问题